MVVM-我将如何在我的主视图模型(和其他视图模型)和我的设置对话框之间传播设置?

发布于 2024-08-29 19:01:00 字数 376 浏览 2 评论 0原文

我正在为我的应用程序构建一个设置对话框,现在所有设置都与主视图模型上的设置相对应,但当我添加更多视图和视图模型时,有些可能不会。

我需要知道将当前设置加载到设置对话框中的最佳实践是什么,然后如果用户单击“确定”,则将设置保存到相应的视图模型中。

我不会使用 Properties.Settings.Default 系统来存储设置,因为我希望我的应用程序尽可能可移植,并且这会将用户范围的设置存储在目录中: C:\Users\ 用户名\Local Settings\Application Data\ ApplicationName 而不是在我的应用程序的目录中。

为了以防万一,我正在使用 Laurent Bugnion 的 MVVM Light Toolkit。

I am building a settings dialog for my application and right now all of the settings correspond with settings on the main view-model, but as I add more view's and view-models some may not.

I need to know what the best practice is for loading the current settings into the settings dialog and then saving the settings to thier corresponding view-models if the user clicks okay.

I will not be using the Properties.Settings.Default system to store settings since I want my application to be as portable as possible and this would store user scoped settings in the directory:
C:\Users\ username \Local Settings\Application Data\ ApplicationName
Instead of in my application's directory.

In case it makes any difference I am using the MVVM Light Toolkit by Laurent Bugnion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈年往事 2024-09-05 19:01:00

使用工具包的 Messenger 来实现怎么样?

当在设置 ViewModel 中进行更改时,您只需通知任何感兴趣的人:

Messenger.Send<Settings>(changedSettings);

所有需要知道设置是否已更改的 Viewmodel 都会注册该消息:

Messenger.Register<Settings>(this, delegate(Settings changedSettings){loadSettings(changedSettings);});

请阅读此处:Mvvm light Messenger 或查看类似的帖子 mvvm-light-how-to-access-property-in-other-view-model< /a>

How about implementing that with the Messenger of the toolkit?

When changes are made in the Settings ViewModel you just inform anyone interested:

Messenger.Send<Settings>(changedSettings);

And all Viewmodels which need to know if settings have been changed register to that message:

Messenger.Register<Settings>(this, delegate(Settings changedSettings){loadSettings(changedSettings);});

Have a read here: Mvvm light messenger or check this similar post mvvm-light-how-to-access-property-in-other-view-model

久隐师 2024-09-05 19:01:00

您可以使用 MEF,从每个视图模型中导出设置视图,并将它们作为视图列表导入,然后添加到堆栈面板或主设置视图中的某些此类视图中。

使用 MEF 的一个很好的信息来源是:http://mef.codeplex.com/wikipage? title=Guide

这是一个我打算早点起床的示例程序:

using System;
使用 System.Collections.Generic;
使用 System.ComponentModel.Composition;
使用 System.ComponentModel.Composition.Hosting;
使用系统反射;

namespace zTestConsole
{
    public interface ISimple
    {
        string Message { get; }
    }

    [Export("SimpleHello",typeof(ISimple))]
    public class SimpleHello : ISimple
    {
        [Export("Message")]
        public string Message
        {
            get { return "Silverlight rocks!"; }
        }
    }

    [Export("SimpleBello",typeof(ISimple))]
    public class SimpleBello : ISimple
    {
        [Export("Message")]
        public string Message
        {
            get { return "C# rocks!"; }
        }
    }

    public class SimpleMultiCat
    {
        [ImportMany("Message")]
        public IEnumerable<string> Messages { get; set; }
    }

    public class SimpleCat
    {
        [Import("SimpleHello")]
        public ISimple simple { get; set; }
    }

    class Program
    {
        private static CompositionContainer container;

        static void Main(string[] args)
        {

            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            SimpleMultiCat cats = new SimpleMultiCat();
            SimpleCat cat = new SimpleCat();

            Program.container = new CompositionContainer(catalog);

            try
            {
                Program.container.ComposeParts(cats);

                foreach (string message in cats.Messages)
                {
                    Console.WriteLine(message);
                }
            }
            catch (CompositionException ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine();

            try
            {
                container.ComposeParts(cat);
                Console.WriteLine(cat.simple.Message);
            }
            catch (CompositionException ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }
    }
}

You could use MEF, exporting a settings view from each view model and importing them as a list of views that you add to a stack panel or some such in your main settings view.

A good source of info of using MEF is: http://mef.codeplex.com/wikipage?title=Guide

Here is a sample program I meant to get up sooner:

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace zTestConsole
{
    public interface ISimple
    {
        string Message { get; }
    }

    [Export("SimpleHello",typeof(ISimple))]
    public class SimpleHello : ISimple
    {
        [Export("Message")]
        public string Message
        {
            get { return "Silverlight rocks!"; }
        }
    }

    [Export("SimpleBello",typeof(ISimple))]
    public class SimpleBello : ISimple
    {
        [Export("Message")]
        public string Message
        {
            get { return "C# rocks!"; }
        }
    }

    public class SimpleMultiCat
    {
        [ImportMany("Message")]
        public IEnumerable<string> Messages { get; set; }
    }

    public class SimpleCat
    {
        [Import("SimpleHello")]
        public ISimple simple { get; set; }
    }

    class Program
    {
        private static CompositionContainer container;

        static void Main(string[] args)
        {

            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            SimpleMultiCat cats = new SimpleMultiCat();
            SimpleCat cat = new SimpleCat();

            Program.container = new CompositionContainer(catalog);

            try
            {
                Program.container.ComposeParts(cats);

                foreach (string message in cats.Messages)
                {
                    Console.WriteLine(message);
                }
            }
            catch (CompositionException ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine();

            try
            {
                container.ComposeParts(cat);
                Console.WriteLine(cat.simple.Message);
            }
            catch (CompositionException ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }
    }
}
墨离汐 2024-09-05 19:01:00

我也遇到过这个问题。对我来说,解决方案是使用类似 ISettingsService 模型的东西。将有 2 个实现。一种用于实际服务,另一种用于设计时间和单元测试。

这里有一个例子:
http://compiledexperience.com/blog/posts/ Blendable-MVVM-依赖注入和单元测试

I've had this problem also. Solution for me was to have something like an ISettingsService Model. There would be 2 implementations. One for the real service and one mocked that was used for design time and unit testing.

An example here:
http://compiledexperience.com/blog/posts/Blendable-MVVM-Dependency-Injection-and-Unit-Testing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文