【问题标题】:Maintain User Control State in UWP Application using Template 10使用模板 10 在 UWP 应用程序中维护用户控制状态
【发布时间】:2016-08-24 11:58:52
【问题描述】:

我正在使用模板 10 创建 UWP 应用。我已经创建了这样的用户控件。

 <my:DeviceInfoUserControl  OnEndpointTypeChange="{Binding OnEndpointTypeChangeCommand}" Component="{Binding DeviceManagementViewModel,Mode=TwoWay}"></my:DeviceInfoUserControl>

我在用户控件上有单选按钮。我在多个屏幕上添加了用户控制。

此用户控件有自己的 ViewModel 以及一些依赖属性,如下所示:

public class DeviceManagementViewModel : ViewModelBase
{

}
public sealed partial class DeviceInfoUserControl : UserControl
{
    public bool IsToggled = true;
    public DeviceInfoUserControl()
    {
        this.InitializeComponent();
    }

    public static readonly DependencyProperty OnEndpointTypeChangeProperty =
      DependencyProperty.Register(
          "OnEndpointTypeChange",
          typeof(ICommand),
          typeof(DeviceInfoUserControl), new PropertyMetadata(null));

    public ICommand OnEndpointTypeChange
    {
        get { return (ICommand)GetValue(OnEndpointTypeChangeProperty); }
        set { SetValue(OnEndpointTypeChangeProperty, value); }
    }

    public static readonly DependencyProperty ComponentProperty = DependencyProperty.Register("Component", typeof(DeviceManagementViewModel), typeof(DeviceInfoUserControl), new PropertyMetadata(null));

    public DeviceManagementViewModel Component
    {
        get { return (DeviceManagementViewModel)GetValue(ComponentProperty); }
        set { SetValue(ComponentProperty, value); }
    }
}

我想在所有屏幕上保留单选按钮选择。我应该如何做到这一点?

【问题讨论】:

  • 那么你想一键选中所有控件上的单选按钮吗?
  • 是的...类似于视图模型的东西可以在全球范围内访问...所以如果我选中单选按钮,它的选择将反映到任何地方
  • SessionState 内置在 ViewModelBase 中
  • 您将需要像 EventAggregator 这样的东西来传递消息。查看 Mvvm Light 示例以及消息传递示例

标签: mvvm uwp template10


【解决方案1】:

您必须确保所有控件实例使用相同的 ViewModel 实例。 XAML 方式总是创建新实例:

<Page.DataContext>
    <vm:DetailPageViewModel x:Name="ViewModel" />
</Page.DataContext>

在带有ResolveForPage方法覆盖的Template10的Bootstrapper类中,您可以通过自定义逻辑或通过依赖注入LINK在页面导航后注入ViewModel的

【讨论】:

    【解决方案2】:

    不知道有没有更好的方法,但我通过制作 Singletone Viewmodel 实现了这一点。

     public class DeviceManagementViewModel : ViewModelBase
     {
        public static readonly DeviceManagementViewModel _instance = new DeviceManagementViewModel ();
        private DeviceManagementViewModel ()
        {
    
        }
    /*Properties and Methods */
    
    }
    

    在父屏幕视图模型中,我创建了以下属性

     private DeviceManagementViewModel  _deviceManagementViewModel;
            public DeviceManagementViewModel DeviceManagementViewModel1
            {
                        get { return _deviceManagementViewModel; }
                        set { Set(ref _deviceManagementViewModel, value); }
            }
    

    我在构造函数中有实例化属性:

      public ConfigurationViewModel()
      {
        DeviceManagementViewModel1 = DeviceManagementViewModel._instance;
      }
    

    在用户控制上:

    <my:DeviceInfoUserControl  OnEndpointTypeChange="{Binding OnEndpointTypeChangeCommand}"  Component="{Binding DeviceManagementViewModel1,Mode=TwoWay}"></my:DeviceInfoUserControl> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-03
      • 2012-01-26
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 2010-12-07
      • 2017-06-09
      • 2017-02-24
      相关资源
      最近更新 更多