使用MVVM进行WPF帧导航绑定 [英] WPF Frame Navigation Binding with MVVM

查看:66
本文介绍了使用MVVM进行WPF帧导航绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有一个使用MVM模式的WPF应用程序。在应用程序中,我有一个框架来托管我的页面a和b,但是当我将视图从页面a更改为b,并尝试将我的视图更改回页面a时,它不会为我带来新页面而是会带回页面一个与旧的国家。我怎么能阻止它?下面是我的xaml和视图模型的代码



xaml



 <   MenuItem    标题  = 运算符   可见性  =  {绑定路径= MenuVisibility,Converter = {StaticResource BooleanToVisibilityConverter}}  

< span class =code-attribute> 命令 = {Binding Path = ViewMenuCommand} / >

< 框架 Grid.Row = 1 名称 = MainAPpFrame NavigationUIVisibility = 隐藏

来源 = {Binding Path = DisplayPage,Mode = TwoWay} Horizo​​ntalContentAlignment = 拉伸 VerticalContentAlignment = 拉伸 / >





我的视图模型



 私人页面displayPage; 

public 页面DisplayPage
{
get
{
return displayPage;
}
set
{
if (displayPage == value
{
return ;
}

.displayPage = value ;
base .OnPropertyChanged( DisplayPage< /跨度>);
}
}

public ICommand ViewMenuCommand
{
get
{
if (_operatorViewMenuCommand == null
{
_operatorViewMenuCommand = new RelayCommand(
param = > this .ProcessNavRequest());
}
return _operatorViewMenuCommand;
}
}

私有 void ProcessNavRequest( )
{
// string uri = String.Format(/ View / {0} ,参数);
.DisplayPage = new OperatorView();
}

解决方案

 private Page _displayPage; 

public Page DisplayPage
{
get
{
return _displayPage;
}
设置
{
if(等于(_displayPage,value))
{
return;
}

this._displayPage = value;
OnPropertyChanged(DisplayPage);
}
}


private RelayCommand _operatorViewMenuCommand;
public ICommand ViewMenuCommand
{
get
{
if(_operatorViewMenuCommand == null)
{
_operatorViewMenuCommand = new RelayCommand(param => ; this.ProcessNavRequest(DisplayPage));
}
返回_operatorViewMenuCommand;
}
}
public void ProcessNavRequest(Page page)
{
// string uri = String.Format(/ View / {0},参数) ;
this.DisplayPage = page;
}





我来自myCommand打电话



 StartScreenViewModel.ProcessNavRequest(new ImportForm()); 





但它不记得导航


Hi guys I have a WPF application that is using MVM pattern. In the application I have a frame that host my page a and b, however when I change view from page a to b, and try to change my view back to page a, it does not bring me a new page instead it brings back page a with its old state. How can I stop that? Below is my code for both my xaml and view model

xaml

<MenuItem Header="Operator" Visibility="{Binding Path=MenuVisibility, Converter={StaticResource BooleanToVisibilityConverter}}"

                          Command="{Binding Path=ViewMenuCommand}" />

<Frame Grid.Row="1" Name="MainAPpFrame" NavigationUIVisibility="Hidden"

                Source="{Binding Path=DisplayPage, Mode=TwoWay}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />



my view model

private Page displayPage;

        public Page DisplayPage
        {
            get
            {
                return displayPage;
            }
            set
            {
                if (displayPage == value)
                {
                    return;
                }

                this.displayPage = value;
                base.OnPropertyChanged("DisplayPage");
            }
        }

public ICommand ViewMenuCommand
        {
            get
            {
                if (_operatorViewMenuCommand == null)
                {
                    _operatorViewMenuCommand = new RelayCommand(
                        param => this.ProcessNavRequest());
                }
                return _operatorViewMenuCommand;
            }
        }

private void ProcessNavRequest()
        {
            //string uri = String.Format("/View/{0}", parameter);
            this.DisplayPage = new OperatorView();
        }

解决方案

private Page _displayPage;

       public Page DisplayPage
       {
           get
           {
               return _displayPage;
           }
           set
           {
               if (Equals(_displayPage, value))
               {
                   return;
               }

               this._displayPage = value;
               OnPropertyChanged("DisplayPage");
           }
       }


       private RelayCommand _operatorViewMenuCommand;
       public ICommand ViewMenuCommand
       {
           get
           {
               if (_operatorViewMenuCommand == null)
               {
                   _operatorViewMenuCommand = new RelayCommand(param => this.ProcessNavRequest(DisplayPage));
               }
               return _operatorViewMenuCommand;
           }
       }
       public void ProcessNavRequest(Page page)
       {
           //string uri = String.Format("/View/{0}", parameter);
           this.DisplayPage = page;
       }



I call from myCommand

StartScreenViewModel.ProcessNavRequest(new ImportForm());



But it does not remember the navigation


这篇关于使用MVVM进行WPF帧导航绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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