繁体   English   中英

我如何从除主要的 class 访问 XAML object?

[英]how do I access a XAML object from a class other than main?

如果我尝试“var mainpage new Mainpage()”,我将运行主页构造函数,然后 XAML object 中的所有字段将返回到 null。 如何访问 silverlight 中来自不同 class 但属于同一命名空间的 XAML 对象?

让我举例说明。 如果您查看第一个答案,这就是我遇到的

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 时,无论用户在程序首次运行时输入的内容都会变成 null。

我首先要尝试的是查看是否在创建 SomeClass 时将值放入该 class 中。

如果失败,我将尝试 MVVM。 我看过http://www.vimeo.com/8915487视频,我得到了示例 mvvm 代码

这是 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;
            }
        }
    }
}

这是视图:

这是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");
                }
            }
        } 
    }
}

使用这个例子,我想知道它是否像注入 ViewModel 然后更改 Model 和 View 中的绑定一样简单。

MVVM 真的这么简单吗?

还有一个。 它是viewmodel基础class

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);
        }
    }
}

好的,现在是困难的部分。 如果我创建一个新的 class。 如何从视图模型 class 获取数据?

首先,让我摆脱这种咆哮:您提出的设计非常糟糕。 它符合臭代码的定义。

如果您坚持这样做,“最佳”方法是在您的页面上声明一些返回实际 UI 元素的公共变量。

<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 模式的东西来实现从 window 到它的视图模型的绑定,然后你可以从视图模型中读取属性值,这样你就可以避免尝试从完全不同的地方触摸任何 UI 元素class。

另一种方法(不走完整的 MVVM 路线)是将必要的值注入到您正在实例化的控件/页面的构造函数中,然后您可以从那里将它们分配给适当的 UI 元素属性。 这仍然很臭,但比直接从外部访问 UI 元素要好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM