如何将 UILabel 与 MvvmCross 绑定
How do I bind a UILabel with MvvmCross
我正在使用 Xamarin 和 MvvmCross 6.4.2 编写一个小型示例应用程序。我完成了 Xamarin.Android 版本,现在开始 Xamarin.iOS 版本。我为第一个屏幕创建了一个视图控制器:
[Register("SignInViewController")]
public class SignInViewController : MvxViewController<SignInViewModel>
{
public SignInViewController() : base(nameof(SignInViewController), null)
{
}
public SignInViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
// Why does it crash when I call base.ViewDidLoad() ?
//base.ViewDidLoad();
NavigationController.NavigationBar.BarTintColor = UIColor.Blue;
var label = new UILabel();
label.Text = "test";
label.BackgroundColor = UIColor.Red;
label.Frame = new CoreGraphics.CGRect(30, 100, 100, 100);
View.Add(label);
// This does not seem to work. If I remove it, label.Text will be "test"
var set = this.CreateBindingSet<SignInViewController, SignInViewModel>();
set.Bind(label).For("Text").To(vm => vm.Username);
set.Apply();
}
}
我贴了一个测试标签只是为了开始并确保一切正常。我希望标签上有文本“用户名测试”,因为我对其进行了硬编码,但是当我 运行 应用程序时它是空白的。此外,如果我尝试使用空引用异常调用 base.ViewDidLoad()
,它会崩溃,这不应该,所以我认为我遗漏了一些重要的东西。
注意:我不想使用 MvvmCross 7,因为它仍在开发中。
请致电 this.Request = MvxViewModelRequest<SignInViewModel>.GetDefaultRequest();
以获取您的第一个视图
:
public override void ViewDidLoad()
{
// Why does it crash when I call base.ViewDidLoad() ?
this.Request = MvxViewModelRequest<SignInViewModel>.GetDefaultRequest();
base.ViewDidLoad();
NavigationController.NavigationBar.BarTintColor = UIColor.Blue;
var label = new UILabel();
label.Text = "test";
label.BackgroundColor = UIColor.Red;
label.Frame = new CoreGraphics.CGRect(30, 100, 100, 100);
View.Add(label);
// This does not seem to work. If I remove it, label.Text will be "test"
var set = this.CreateBindingSet<SignInViewController, SignInViewModel>();
set.Bind(label).For("Text").To(vm => vm.Username);
set.Apply();
}
参考:how-to-call-mvxdialogviewcontroller-as-a-popover)
经过一些研究,我发现真正的问题是我没有正确地使用 iOS/MvvmCross 做事。我想用 C# 编写所有视图的代码并避免使用 storyboard/xib/designer class,但必须先更改一些内容。已接受的答案 为我解决了所有问题。
我正在使用 Xamarin 和 MvvmCross 6.4.2 编写一个小型示例应用程序。我完成了 Xamarin.Android 版本,现在开始 Xamarin.iOS 版本。我为第一个屏幕创建了一个视图控制器:
[Register("SignInViewController")]
public class SignInViewController : MvxViewController<SignInViewModel>
{
public SignInViewController() : base(nameof(SignInViewController), null)
{
}
public SignInViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
// Why does it crash when I call base.ViewDidLoad() ?
//base.ViewDidLoad();
NavigationController.NavigationBar.BarTintColor = UIColor.Blue;
var label = new UILabel();
label.Text = "test";
label.BackgroundColor = UIColor.Red;
label.Frame = new CoreGraphics.CGRect(30, 100, 100, 100);
View.Add(label);
// This does not seem to work. If I remove it, label.Text will be "test"
var set = this.CreateBindingSet<SignInViewController, SignInViewModel>();
set.Bind(label).For("Text").To(vm => vm.Username);
set.Apply();
}
}
我贴了一个测试标签只是为了开始并确保一切正常。我希望标签上有文本“用户名测试”,因为我对其进行了硬编码,但是当我 运行 应用程序时它是空白的。此外,如果我尝试使用空引用异常调用 base.ViewDidLoad()
,它会崩溃,这不应该,所以我认为我遗漏了一些重要的东西。
注意:我不想使用 MvvmCross 7,因为它仍在开发中。
请致电 this.Request = MvxViewModelRequest<SignInViewModel>.GetDefaultRequest();
以获取您的第一个视图
:
public override void ViewDidLoad()
{
// Why does it crash when I call base.ViewDidLoad() ?
this.Request = MvxViewModelRequest<SignInViewModel>.GetDefaultRequest();
base.ViewDidLoad();
NavigationController.NavigationBar.BarTintColor = UIColor.Blue;
var label = new UILabel();
label.Text = "test";
label.BackgroundColor = UIColor.Red;
label.Frame = new CoreGraphics.CGRect(30, 100, 100, 100);
View.Add(label);
// This does not seem to work. If I remove it, label.Text will be "test"
var set = this.CreateBindingSet<SignInViewController, SignInViewModel>();
set.Bind(label).For("Text").To(vm => vm.Username);
set.Apply();
}
参考:how-to-call-mvxdialogviewcontroller-as-a-popover)
经过一些研究,我发现真正的问题是我没有正确地使用 iOS/MvvmCross 做事。我想用 C# 编写所有视图的代码并避免使用 storyboard/xib/designer class,但必须先更改一些内容。已接受的答案