开发者

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

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

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

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

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

Here is the 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:

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.

Is MVVM really this easy?

There is one more. It is the viewmodel base class

using System.ComponentModel;

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

        public void R开发者_StackOverflow中文版aisePropertyChanged(string PropertyName)
        {
            var e = new PropertyChangedEventArgs(PropertyName);
            PropertyChangedEventHandler changed = PropertyChanged;
            if (changed != null) changed(this, e);
        }
    }
}

OK, so now the hard part. If I create a new class. How do I get the data from the viewmodel class?


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

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

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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜