页面无法在多页面WPF / MVVM应用程序中保留UI更改

时间:2018-07-22 13:48:29

标签: c# wpf mvvm-light

我有一个简单的多页MVVM Light应用程序,我的问题是,如果我在屏幕上绘制某些内容或更改其中一个视图/页面中按钮的背景颜色,然后转到另一个页面并返回,绘制的内容不再存在。

enter image description here

主页面代码:

XAML

<Window x:Class="TwoViews.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"
        Title="MVVM Light View Switching"
        d:DesignHeight="300"
        d:DesignWidth="300"
        DataContext="{Binding Main,
                              Source={StaticResource Locator}}"
        ResizeMode="NoResize"
        SizeToContent="WidthAndHeight"
        mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ContentControl Content="{Binding CurrentViewModel}" />

        <DockPanel Grid.Row="1" Margin="5">
            <Button Width="75"
                    Height="23"
                    Command="{Binding SecondViewCommand}"
                    Content="Second View"
                    DockPanel.Dock="Right" />
            <Button Width="75"
                    Height="23"
                    Command="{Binding FirstViewCommand}"
                    Content="First View"
                    DockPanel.Dock="Left" />
        </DockPanel>
    </Grid>
</Window>

ViewModel

using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace TwoViews.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private ViewModelBase _currentViewModel;
        readonly static SecondViewModel _secondViewModel = new SecondViewModel();

        readonly static FirstViewModel _firstViewModel = new FirstViewModel();
        public ViewModelBase CurrentViewModel
        {
            get
            {
                return _currentViewModel;
            }
            set
            {
                if (_currentViewModel == value)
                    return;
                _currentViewModel = value;
                RaisePropertyChanged("CurrentViewModel");
            }
        }

        public ICommand FirstViewCommand { get; private set; }
        public ICommand SecondViewCommand { get; private set; }

        public MainViewModel()
        {
            CurrentViewModel = MainViewModel._firstViewModel;
            FirstViewCommand = new RelayCommand(() => ExecuteFirstViewCommand());
            SecondViewCommand = new RelayCommand(() => ExecuteSecondViewCommand());
        }         

        private void ExecuteFirstViewCommand()
        {
            CurrentViewModel = MainViewModel._firstViewModel;
        }
        private void ExecuteSecondViewCommand()
        {
            CurrentViewModel = MainViewModel._secondViewModel;
        }
    }
}

隐藏代码

namespace TwoViews
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

第一页代码:

XAML

<UserControl x:Class="TwoViews.Views.FirstView"
             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"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <Grid>
        <Label x:Name="label" Content="First Page" HorizontalAlignment="Left" Margin="92,46,0,0" VerticalAlignment="Top"/>
    </Grid>
</UserControl>

ViewModel

namespace TwoViews.ViewModels
{
    public class FirstViewModel : ViewModelBase
    {
        public FirstViewModel()
        {
        }
    }
}

隐藏代码

namespace TwoViews.Views
{
    public partial class FirstView : UserControl
    {
        public FirstView()
        {
            InitializeComponent();
        }
    }
}

第二页代码

XAML

<UserControl x:Class="TwoViews.Views.SecondView"
             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"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <Grid Name="myGrid">
        <Grid Name="GridSecondPage" HorizontalAlignment="Left" Height="107" Margin="10,43,0,0" VerticalAlignment="Top" Width="280"/>
        <Button x:Name="DrawRectangle" Content="Draw" HorizontalAlignment="Left" Margin="135,203,0,0" VerticalAlignment="Top" Width="75" Click="DrawRectangle_Click"/>
    </Grid>
</UserControl>

ViewModel

namespace TwoViews.ViewModels
{
    public class SecondViewModel : ViewModelBase
    {
    }
}

隐藏代码

namespace TwoViews.Views
{
    public partial class SecondView : UserControl
    {
        public SecondView()
        {
            InitializeComponent();
        }
        private void DrawRectangle_Click(object sender, RoutedEventArgs e)
        {
            Rectangle myRectangle = new Rectangle();
            myRectangle.Name = "myRectangle";
            myRectangle.Width = 100;
            myRectangle.Height = 100;
            myRectangle.Fill = Brushes.Blue;
            GridSecondPage.Children.Add(myRectangle);
        }
    }
}

0 个答案:

没有答案