视图模型未更新 [英] View Model not updating

查看:31
本文介绍了视图模型未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在练习如何在 C# 上使用 WPF,我已经掌握了它的基础知识但我面临一个问题,我设置了我的代码以在单击按钮时打开一个新窗口但是视图没有更新到窗口 2应该看起来它只是与 window1 显示相同的视图,但是从一个窗口到另一个窗口的更改正在工作,只是内容没有改变.

Im currently practicing how to use WPF on C#, ive gotten the basics of it but im facing an issue, ive set up my code to open a new window when a button is clicked however the view does not update to how window 2 is supposed to look it just shiws the same view as window1 but the changing from window to window is working just the content is not changing.

窗口一 cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PROG6221_POE_Tarisai_Gonah_18028480
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

    

        private void OpenWindow(object sender, RoutedEventArgs e)
        {
            Task1 f1 = new Task1();
            this.Visibility = Visibility.Hidden;
            f1.Show();
        }
    }
}

窗口 2 cs:

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PROG6221_POE_Tarisai_Gonah_18028480
{
    /// <summary>
    /// Interaction logic for Task1.xaml
    /// </summary>
    public partial class Task1 : Window
    {
        public Task1()
        {
            InitializeComponent();
        }

        private void OpenWindow(object sender, RoutedEventArgs e)
        {
            //Expenses objexp = new Expenses();
            //this.Visibility = Visibility.Hidden;
            //objexp.Show();

            MainWindow f1 = new MainWindow();
            this.Visibility = Visibility.Hidden;
            f1.Show();


        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {

        }
    }
}

Window 1 xaml 和 window 2 xaml 完全不同,但是当我单击按钮时,它确实显示了 windows 2 设计.

Window 1 xaml and window 2 xaml are completely different but when i click the button it does show the windows 2 design.

窗口 1 xaml:

<Window x:Class="PROG6221_POE_Tarisai_Gonah_18028480.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PROG6221_POE_Tarisai_Gonah_18028480"
        mc:Ignorable="d" FontSize="18"
        Title="Finance Savings Calculator" Height="550" Width="900">
    <Grid RenderTransformOrigin="0.529,0.484" Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" OpacityMask="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}">

        <Grid.ColumnDefinitions>


            <ColumnDefinition Width="46*"/>
            <ColumnDefinition Width="0*"/>
            <ColumnDefinition Width="179*"/>


        </Grid.ColumnDefinitions>
        <TextBlock HorizontalAlignment="Left" Text="                                                    Welcome to the finance calculator" TextWrapping="Wrap" Width="900" FontSize="24" FontWeight="Bold" Margin="0,10,0,486" Foreground="Black" Background="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Grid.ColumnSpan="3" FontFamily="Imprint MT Shadow" TextDecorations="{x:Null}"/>

        <Button Content="HOME" HorizontalAlignment="Center" VerticalAlignment="Top" Width="184" Height="38" Click="Button_Click" Background="#FF20D6FF" FontWeight="Bold" BorderBrush="{DynamicResource {x:Static SystemColors.InactiveCaptionTextBrushKey}}" Margin="0,48,0,0"/>


        <Button Content="Task 1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="184" Height="38" Click="OpenWindow" Background="#FF20D6FF" FontWeight="Bold" Margin="0,118,0,0" Foreground="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>
        <TextBlock Grid.Column="2" HorizontalAlignment="Left" Height="59" Margin="39,89,0,0" Text="This is a finance calculator. This app is designed to track your monthly expenses and income" TextWrapping="Wrap" VerticalAlignment="Top" Width="677" FontFamily="Arial Black"/>

    </Grid>
</Window>

窗口 2 xaml:

<Window x:Class="PROG6221_POE_Tarisai_Gonah_18028480.Task1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PROG6221_POE_Tarisai_Gonah_18028480"
             mc:Ignorable="d" FontSize="18"
        Title="Finance Savings Calculator" Height="550" Width="900">
    <Grid RenderTransformOrigin="0.529,0.484" Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" OpacityMask="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}">

        <Grid.ColumnDefinitions>


            <ColumnDefinition Width="46*"/>
            <ColumnDefinition Width="0*"/>
            <ColumnDefinition Width="179*"/>


        </Grid.ColumnDefinitions>
        <TextBlock HorizontalAlignment="Left" Text="                                                    Welcome to the finance calculator" TextWrapping="Wrap" Width="900" FontSize="24" FontWeight="Bold" Margin="0,10,0,486" Foreground="Black" Background="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Grid.ColumnSpan="3" FontFamily="Imprint MT Shadow" TextDecorations="{x:Null}"/>

        <Button Content="HOME" HorizontalAlignment="Center" VerticalAlignment="Top" Width="184" Height="38" Click="OpenWindow" Background="#FF20D6FF" FontWeight="Bold" BorderBrush="{DynamicResource {x:Static SystemColors.InactiveCaptionTextBrushKey}}" Margin="0,48,0,0"/>


        <Button Content="Task 1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="184" Height="38" Click="ButtonClick" Background="#FF20D6FF" FontWeight="Bold" Margin="0,118,0,0" Foreground="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>
        <Rectangle Grid.Column="2" HorizontalAlignment="Center" Height="427" Stroke="Black" VerticalAlignment="Center" Width="696"/>
        <TextBox Grid.Column="2" HorizontalAlignment="Left" Margin="305,195,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox Grid.Column="2" HorizontalAlignment="Left" Margin="305,291,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label Content="Gross Income:" Grid.Column="2" HorizontalAlignment="Left" Margin="178,187,0,0" VerticalAlignment="Top"/>
        <Label Content="Tax:" Grid.Column="2" HorizontalAlignment="Left" Margin="261,287,0,0" VerticalAlignment="Top"/>
        <Button Content="Next" Grid.Column="2" HorizontalAlignment="Left" Height="33" Margin="261,408,0,0" VerticalAlignment="Top" Width="205" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}" Click="OpenWindow"/>

    </Grid>
</Window>

我被告知 INOTIFYPROPERTY 更改是缺少代码,但我完全不知道如何在我的代码中实现它.

I was told that INOTIFYPROPERTY change is the code missing but i am completely lost to how i would implement it in my code.

推荐答案

将 Wall 窗口作为参数发送到第二个窗口,在其中将 Visibility 属性设置为 Visibile,如果Task1创建一次,就不需要创建多个实例.

Send the Wall window as a parameter to the second window, where you set the Visibility property to Visibile, and if Task1 is created once, there is no need to create multiple instances.

更改的完整代码如下.

将主窗口更改为:

namespace PROG6221_POE_Tarisai_Gonah_18028480
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Task1 f1;
        public MainWindow()
        {
            InitializeComponent();
        }
    

        private void OpenWindow(object sender, RoutedEventArgs e)
        {
            if(f1 == null)
            {
               f1 = new Task1(this);
               f1.Show();
            }
            else
               f1.Visibility = Visibility.Visible;
            this.Visibility = Visibility.Hidden;
        }
    }
}

将 Task1 更改为:

change Task1 to this :

namespace PROG6221_POE_Tarisai_Gonah_18028480
{
    /// <summary>
    /// Interaction logic for Task1.xaml
    /// </summary>
    public partial class Task1 : Window
    {
        MainWindow mainWindow;
        public Task1(MainWindow _mainWindow)
        {
            InitializeComponent();
            mainWindow = _mainWindow
        }

        private void OpenWindow(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            mainWindow..Visibility = Visibility.Visible;
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {

        }
    }
}

这篇关于视图模型未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆