0

I’m trying to set up a Xamarin.Forms solution using the three assemblies „Model“, „ViewModel“ and „View“ to separate my code according to MVVM style like i did often before in Windows WPF apps.

The iOS and Android bootstrapper projects have both references to each of the three assemblies View-Assembly has a reference to ViewModel and Model, ViewModel only references Model.

I added a simple „MainPage“ in View-Assembly and the matching „MainViewModel“ in ViewModel-Assembly. Everything compiles fine and the Xamarin.Forms-Previewer in Visual Studio shows me a preview where a Label-Text is shown via binding to the ViewModel.

On Android simulator the app works as expected. On iOS it crashes when it tries to show the Page.

I guess this has a lot to do with: iOS is using ahead of time compiling. Or maybe i do something wrong in defining the xmlns tag in xaml.

Unhandled Exception: Xamarin.Forms.Xaml.XamlParseException: Position 8:10. Type vm:MainPageViewModel not found in xmlns clr-namespace:MyApp3.ViewModel;assembly=MyApp3.ViewModel

Code:

namespace MyApp3.ViewModel
{
    public class MainPageViewModel
    {
        public string MyText { get; set; } = "Some Text";
    }
}

And Page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyApp3"
             xmlns:vm="clr-namespace:MyApp3.ViewModel;assembly=MyApp3.ViewModel"
             x:Class="MyApp3.View.MainPage">
    <ContentPage.BindingContext>
        <vm:MainPageViewModel />
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <Label Text="{Binding MyText}"
                   VerticalOptions="CenterAndExpand" 
                   HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Do i just miss some hidden setting or is this a limitation so i have to keep my code inside one Assembly?

1 Answer 1

0

Yes you can do it, check the names. Check if the assembly name of view models is named MyApp3.ViewModel (check it in the properties of the project).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.