1
votes

I have a pcl dll (Xamarin) and all the viewmodels, models, services, interfaces, converters are on this project.

I am already using this core on Android and IOS and now will start the Windows Phone app.

My main question for now is, how do I make a viewmodel the datacontext for a view. For sample: LoginViewModel.cs (core) and LoginView.xaml...

I am using MVVMCross and The Windows Phone project is 8.1

Than you in advance.

Updating:

I tried this:

<views:MvxWindowsPage
    x:Class="Tocalivros.WindowsPhone.Views.LoginView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Tocalivros.WindowsPhone.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:views="using:Cirrious.MvvmCross.WindowsCommon.Views"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">


public sealed partial class LoginView : MvxWindowsPage<LoginViewModel>
    {
        public LoginView()
        {
            this.InitializeComponent();
        }

But now I get this error: "Severity Code Description Project File Line Error CS0263 Partial declarations of 'LoginView' must not specify different base classes"

Based on samples, I tried a different aproach, instead of making the view of type view model, I create a ViewModel as a property and set it as DataContext for the view. Like this:

public sealed partial class LoginView : MvxWindowsPage { public new LoginViewModel ViewModel { get { return (LoginViewModel)base.ViewModel; } set { base.ViewModel = value; } }

        public LoginView()
        {
            this.InitializeComponent();
            this.DataContext = ViewModel;
        }

But this way the viewmodel wont be initiated with the required parameters... (IServices)

It makes me think the better aproach is the first:

public sealed partial class LoginView : MvxWindowsPage<LoginViewModel> but I get error...
1

1 Answers

1
votes

You can do like with every other platform in MvvmCross.

public class LoginPage : MvxWindowsPage<LoginViewModel>
{
}

Inside your view you will see something like this:

<views:MvxWindowsPage
    x:Class="somenamspace.Views.LoginPage">
<!-- Put your layout here -->
</views:MvxWindowsPage>

The datacontext is now done for that view.