我怎么从一个类访问XAML对象以外的主? [英] how do I access a XAML object from a class other than main?

查看:120
本文介绍了我怎么从一个类访问XAML对象以外的主?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试无功炫魅新的网主页()
我将运行炫魅构造函数,然后在XAML对象中的所有字段将返回空值。如何访问我在Silverlight是从不同的类,但相同的命名空间的一部分XAML对象?

If I try "var mainpage new Mainpage()" I will run the mainpage constructor and then all the fields in the XAML object will return to null. How to I access XAML objects in silverlight that are from a different class but part of the same namespace?

让我通过例子来说明。如果你看一下第一个答案,这里是我所遇到

Let me explain by example. If you look at the first answer, here is what I am encountering

public class MyPage
{
    MyPage()
    {

      // the constructor makes all the variables from the xaml null
    }

    public TextBox MyTextBox
    {
        get { return SomeTextBox; }
    }
}


public class SomeOtherClass
{
    private void SomeFunction()
    {
        var page = new MyPage();   // this makes the text empty
        var sometext = page.MyTextBox.Text;   // so sometext will be empty
    }
}



所以,无论用户当我运行SomeFunction当程序第一次运行imputs变为空。

So whatever the user imputs when the program first runs turns to null when I run SomeFunction.

什么,我第一次去尝试,就是看是否创建SomeClass的时候,其值将投入该类。

What I am first going to try is to see if when SomeClass is created, the values are put into that class.

如果失败了,我要去尝试MVVM。我见过的 http://www.vimeo.com/8915487 视频和我得到的样本代码MVVM

If that fails, I am going to try MVVM. I have seen the http://www.vimeo.com/8915487 video and I got the sample mvvm code

下面是Model:

namespace SimpleMVVM.Model
{
    public class SimpleModel
    { 
        // super easy version
        //public string SomeSimpleValue { get; set; }

        private string _SomeSimpleValue = string.Empty;

        // actually do something version...
        public string SomeSimpleValue
        {
            get
            {
                return "some value";
            }
            set
            {
                _SomeSimpleValue = value;
            }
        }
    }
}



在这里认为:

here is the view:








和这里是viewmodel.cs

and here is the viewmodel.cs

using Simple;
using SimpleMVVM.Model;

namespace SimpleMVVM.ViewModel
{
    public class SimpleViewModel : SimpleViewModelBase
    {
        private SimpleModel MyModel = new SimpleModel(); 

        public string SomeSimpleValue
        {
            get { return MyModel.SomeSimpleValue; }
            set
            {
                if (MyModel.SomeSimpleValue != value)
                {
                    MyModel.SomeSimpleValue = value;
                    RaisePropertyChanged("SomeSimpleValue");
                }
            }
        } 
    }
}

使用这个例子,我想知道如果它只是那么容易,因为注射视图模型,然后改变在模型和视图绑定。

Using this example, I am wondering if it will just as easy as injecting a ViewModel and then changing the bindings in the Model and the View.

时MVVM实际上这容易吗?

Is MVVM really this easy?

还有一个。这是

using System.ComponentModel;

namespace Simple
{
    public class SimpleViewModelBase : INotifyPropertyChanged
    { 
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string PropertyName)
        {
            var e = new PropertyChangedEventArgs(PropertyName);
            PropertyChangedEventHandler changed = PropertyChanged;
            if (changed != null) changed(this, e);
        }
    }
}

确定,所以现在最困难的部分。如果我创建一个新的类。我如何从视图模型类获得数据?

推荐答案

首先,让我得到这个咆哮出路:的你打算什么很糟糕的设计。它适合臭代码的定义。

First, let me get this rant out of the way: what you propose is very bad design. It fits the definition of smelly code.

如果你坚持这样做,最好的方式采取的是申报返回实际的UI元素在页面上的一些公共变量。

If you insist on doing it this way, the "best" approach to take is to declare some public variables on your page that return the actual UI elements.

<UserControl x:Class="MyNamespace.MyPage"  ...>
    <Grid>
        <TextBox x:Name="SomeTextBox" Width="100" />
    </Grid>
</UserControl>


public class MyPage
{
    public TextBox MyTextBox
    {
        get { return SomeTextBox; }
    }
}


public class SomeOtherClass
{
    private void SomeFunction()
    {
        var page = new MyPage();
        page.MyTextBox.Text = "some text";
    }
}



当然首选的方法是使用类似MVVM模式实现从窗口到视图模型绑定,那么你可以从视图模型读取属性值,这样就避免试图从一个完全不同的类接触任何UI元素。

Of course the preferred method would be to use something like the MVVM pattern to implement binding from your window to its viewmodel, then you can just read the property values from the viewmodel, this way you avoid trying to touch any UI elements from a totally different class.

另一种方式来做到这一点(没有去充分MVVM路线)是注入必要的值到控制/页,你是实例化的构造,并从那里您可以将它们分配给适当的UI元素属性。这仍然是臭,但不是直接访问来自外部的UI元素更好

Another way to do it (without going the full MVVM route) is to inject the necessary values into the constructor of the control/page that you are instantiating, and from there you can assign them to the appropriate UI element properties. This is still smelly, but better than directly accessing the UI elements from the outside.

这篇关于我怎么从一个类访问XAML对象以外的主?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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