【问题标题】:How to inherit ViewModel class to UserControl?如何将 ViewModel 类继承到 UserControl?
【发布时间】:2018-02-04 02:33:55
【问题描述】:

假设我有一个具有不同属性的ViewModel 类,例如:

//MainVm will inherit ViewModel that contains IPropertyChanged implementation
public class MainVM : ViewModel 
{
     publi bool Foo { get; set; } 
}

我以这种方式在主controller 上设置了这个类:

MainController mc;

public MaiWindow()
{
    mc = new MainController(); 
    DataContext = mc;
    InitializeComponent();
}

MainController 有这个实现:

public class MainController : MainVM 
{
    some methods
}

所以每次我需要访问每个 UserControls 上的 MainController 的属性时,我需要这样做:Application.Current.MainWindow.mc.Foo

这一点也不优雅。

不调用上面的代码行是否可以访问特定ViewModel的属性?

谢谢。

更新

要为这个问题添加更多详细信息和说明:所以在我的 UserControl 中,我需要访问属于MainVMFoo 属性。我谈到了UserControl xaml 代码(不是 UserControl 的控制器),这是一个示例:

public partial class ControlName : UserControl
{
    public ControlName()
    {
       InitializeComponent();
    }

    public btnAdd_Click(object sender, RoutedEventArgs e)
    {
       //Suppose that I need to access to the property Foo of MainVM here
       //I need to access to MainWindow instance like this:
       Application.Current.MainWindow.mc.Foo

       //isn't possible instead access to the property directly inherit the class, like:
       MainVm.Foo 
    }
}

【问题讨论】:

  • 你能说得更具体些吗?您需要在应用程序的哪个部分使用“Application.Current.MainWindow.mc.Foo”?如果您的代码在控制器中,则不需要 Application.Current,而且我很难看到您需要在外部应用程序中访问控制器的位置。如果您可以在问题的最后一部分添加更多代码。
  • @PhillipH 确保检查更新
  • 嗨。我猜你不理解 MVVM 背后的想法。请先检查msdn.microsoft.com/en-us/library/hh848246.aspx,然后尝试使用框架或工具包,如mvvmlight.net/creating
  • @Marco 我已经实现了 MVVM 逻辑,我只是​​问如何在不通过 MainWindow 实例的情况下访问特定控制器的属性
  • 嗯,你正在使用 btnAdd_Click 告诉我它仍然不是 MVVM。无论如何,在 ControlName 中,您可以将 DataContext 强制转换回 MainController 或 MainVM ......这很糟糕,但它会起作用。我仍然建议阅读我发送的文档,您发布的代码不是 MVVM

标签: c# wpf mvvm user-controls


【解决方案1】:

为了获得应用范围内的配置,您可以使用

  ConfigurationManager.AppSettings["Whatever"];

这些设置位于 App.Config 中,格式如下

<appSettings>
    <add key="Whatever" value="Hello" />
</apSettings>

但据我所知,您有一个 ViewModel 可以让用户更改设置,在这种情况下您应该选择:

Properties.Settings.Default.myColor = Color.AliceBlue; 

您的 ViewModel 可以将此属性公开为:

public Color MyColor
{
    get {
       return Properties.Settings.Default.myColor;
    }

    set {
        Properties.Settings.Default.myColor = value; RaisePropertyChanged();
    }
}

public void Persist()
{
    Properties.Settings.Default.Save();
    // Raise whatever needed !
}

您也可以从其他 ViewModel 访问这些设置:

Properties.Settings.Default.myColor

看看这里https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings和这里https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-create-application-settings

【讨论】:

  • 好的,但是假设属性是通过读取 json 来填充的,我只能使用 settingsVm 实例(包含这些设置的类的名称)来访问属性值,所以我不得不调用Application.Current.MainWindow.mc.SettingsVm.PropertySettingsName,你同意吗?没有其他方法可以做到这一点
  • 不。 Properties.Settings.Default 在所有应用程序中都可用...如果您愿意,可以将其称为“全局变量”。如果您选择用户范围,则设置将保存在每个用户配置文件目录中,更具体地说,保存在用户配置文件的 AppData 文件夹中名为 user.config 的文件中。完整路径取决于应用程序。在 Windows 7 中,它类似于 C:\Users[user]\AppData\Local[YourApp] 如果您选择应用范围,则 C:\users\public\appdata\local
  • 顺便说一句,文件是 xml。比如 True值>
  • 我知道,但我的只是一个解释情况的例子......问题的本质是:如何从另一个视图模型类访问特定视图模型类的属性而不通过通过 MainWindow 实例?在这里我很困惑,希望得到答复,感谢您到目前为止所投入的时间:)
  • 这个很简单 ;-) msdn.microsoft.com/en-us/magazine/jj694937.aspx - 使用信使
猜你喜欢
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多