"var mainpage new Mainpage()"시도 메인 페이지 생성자를 실행하면 XAML 개체의 모든 필드가 null로 반환됩니다. Silverlight에서 다른 클래스이지만 같은 네임 스페이스의 일부인 XAML 개체에 액세스하는 방법main이 아닌 클래스에서 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을 실행할 때 어떤 프로그램을 먼저 실행 사용자 imputs가 null로 전환하는 것이다.
내가 처음 시도하는 것은 SomeClass가 만들어지면 그 클래스에 값이 들어 있는지 확인하는 것입니다.
이것이 실패하면 MVVM을 사용해 보겠습니다. 보기 여기
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;
}
}
}
}
됩니다 :
와 나는 http://www.vimeo.com/8915487 비디오를 본 나는 샘플 MVVM 코드 여기
을받은 모델입니다 여기에 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을 주입 한 다음 모델 및 뷰에서 바인딩을 변경하는 것만 큼 쉬운지 궁금합니다.
MVVM이 정말 쉽습니까?
하나 더 있습니다. 그것은 그래서 지금, 어려운 부분 뷰 모델의 기본 클래스
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);
}
}
}
OK입니다. 새로운 수업을 만들면. viewmodel 클래스에서 데이터를 얻으려면 어떻게해야합니까?
예, MVVM은 간단합니다 (버튼 클릭과 같은 UI 동작을 처리해야 할 때 더욱 복잡해집니다). 개인적으로 위의 MVVM 패턴에 동의하지 않지만 지금은 수행 할 것입니다. 추가 질문이 있으면 새로운 질문을 올리십시오. – slugster