MVVM 类实例化和参数

发布于 2024-11-24 08:31:51 字数 269 浏览 2 评论 0原文

我正在尝试让我的大脑进入 MVVM 模式。

我的视图中有一个网格,以及在我的视图模型中操作它并向其添加形状的各种方法。我意识到我应该尽量避免在虚拟机中发生这种情况,所以我将这些方法移到了自己的类中。我想弄清楚的是

a) 应该在哪里创建新类的实例?目前我已经在我的虚拟机中使用了 IoC.Get()

b) NewClass 需要获取视图中的 Grid,我该怎么做? (我唯一能想到的就是让虚拟机获取对其视图的引用,并传递网格 进入 NewClass,但这似乎不是最好的方法)

I'm trying to whip my brain into MVVM mode here.

I have a Grid in my view, and various methods for manipulating it and adding shapes to it in my viewmodel. I realize that I should try to avoid that sort of thing in the VM, so I moved those methods into there own class. What I'm trying to figure out is

a) Where should an instance of the new class be created? Currently I've got it in my VM, using
IoC.Get()

b) The NewClass needs to get a hold of the Grid in the view, how can I do that?
(The only thing I can think of is to have the VM get a reference to its View, and pass the Grid
into the NewClass, but that doesn't seem like the best way to do it)

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

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

发布评论

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

评论(1

倦话 2024-12-01 08:31:51

A) 如果您需要以无法通过视图模型的方式操作网格,请考虑使用协程。对视图的引用在 ActionExecutionContext 中传递。

public interface IResult
{
    void Execute(ActionExecutionContext context);
    event EventHandler<ResultCompletionEventArgs> Completed;
}

public class ActionExecutionContext
{
    public ActionMessage Message;
    public FrameworkElement Source;
    public object EventArgs;
    public object Target;
    public DependencyObject View;
    public MethodInfo Method;
    public Func<bool> CanExecute;
    public object this[string key];
}

A) look at using Coroutines if you need to manipulate the grid in a way you're not able to through the viewmodel. A reference to the view gets passed in the ActionExecutionContext.

public interface IResult
{
    void Execute(ActionExecutionContext context);
    event EventHandler<ResultCompletionEventArgs> Completed;
}

public class ActionExecutionContext
{
    public ActionMessage Message;
    public FrameworkElement Source;
    public object EventArgs;
    public object Target;
    public DependencyObject View;
    public MethodInfo Method;
    public Func<bool> CanExecute;
    public object this[string key];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文