visual studio xamarin forms mvvm

3

我正在启动一个新项目,想要使用MVVM框架-我非常喜欢这种模式,并且在所有的Windows Phone 8.1应用中都使用过。但是转向Xamarin是一件很困难的事情!我通常使用MVVM Light,并且有一个不错的基本实现,每次创建新项目时都会使用它,但是我找不到一个真正好的示例来展示我需要的东西。

我想做的是创建一个Xamarin共享(或可移植)项目,使视图可以跨越所有平台共享。我想使用代码后台创建视图-所以没有XAML。

有没有人有这方面的经验,能指导我寻找一个好的样例呢?我也在想是否终究需要使用第三方框架,因为导航似乎很容易。


你为什么想要这样做呢?XAML 的主要优点是快速简便地设计用户界面和轻松绑定。 - Tseng
我不喜欢 XAML。我更喜欢尽可能使用一种语言。在我看来,这样长期维护起来更容易。 - Wosi
1个回答

1

有许多相关样例可以找到。我最喜欢的Xamarin.Forms样例网站是Xamarin Forms in Anger

让我们来看看Jobbberr 样例

using System;
using Xamarin.Forms;

namespace InAnger.Jobbberr
{
    public class SettingsPage : ContentPage
    {
        public SettingsPage ()
        {
            Style = AppStyle.SettingsPageStyle;

            var pageTitle = new Frame () {
                Style = AppStyle.PageTitleLabelFrameStyle,
                Padding = new Thickness(0,Device.OnPlatform(15,0,0),0,10),
                Content = new Label { 
                    Style = AppStyle.PageTitleLabelStyle,
                    Text = "Settings",
                }
            };

            var signoutButton = new Button () {
                VerticalOptions = LayoutOptions.EndAndExpand,
                HorizontalOptions = LayoutOptions.Center,
                Text = "Sign Out",
                TextColor = AppStyle.DarkLabelColor,
            };

            Content = new StackLayout {
                VerticalOptions = LayoutOptions.FillAndExpand,
                Padding = new Thickness (20),
                Children = {
                    pageTitle,
                    new BoxView() {
                        HeightRequest = 1,
                        BackgroundColor = AppStyle.DarkLabelColor,
                    },
                    new SettingsUserView(),
                    new SyncView (),
                    new SettingsSwitchView ("GPS"),
                    new SettingsSwitchView ("Jobs Alert"),
                    signoutButton,
                    new StatusBarView()
                }
            };
        }
    }
}

这里你看到了什么?

新的类SettingsPage派生自ContentPage。控件pageTitlesignoutButton在其构造函数中创建。最后可以看到如何创建一个StackLayout,并用控件填充它,并将其设置为页面的内容。这就是在代码中创建Page的方法。

如何应用MVVM?

  1. Set BindingContext = ViewModel in the first row of the constructor (create a new view model or locate it by via a ViewModelLocator or anything).

  2. Let's say for example you want to bind the Text and Command property of signoutButton to the view model's properties SignOutButtonText and SignoutCommand. You would change the creation of the button to this:

    var signoutButton = new Button () {
        VerticalOptions = LayoutOptions.EndAndExpand,
        HorizontalOptions = LayoutOptions.Center,
        TextColor = AppStyle.DarkLabelColor,
    };
    
    signoutButton.SetBinding(Button.TextProperty, "SignOutButtonText");
    signoutButton.SetBinding(Button.CommandProperty, "SignoutCommand");
    

谢谢你的回答。我之前不知道 Anger 中有表单 :) 我一直在研究并尝试它,感觉非常好。我的问题不是制作简单的 MVVM 应用程序,而是当涉及到导航和在视图模型之间传递参数时会变得复杂。我现在的解决方案是将参数传递给视图,然后在视图模型上执行所需的操作,添加数据等。但我感觉这里打破了模式 - 因为存在更高的耦合性。通常我会让视图实现一个导航接口来构造视图模型。 - OneBigQuestion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接