Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.8k stars 778 forks source link

Calburn.Micro with Xamarin Forms #702

Closed asWorks closed 4 years ago

asWorks commented 4 years ago

Hello, I am trying to use Calibur.Micro with Xamarin.Forms. After searching the documentation, Stack Overflow and Google/Youtube I can´t find any newer samples.

I follow the steps written down in the samples/Setup part of this repo.

I fail after trying to inherit app.xaml.cs from 'FormsApplication' - getting either the error: "Missing partial modifier on declaration of type 'App'; another partial declaration of this type exists TestCM" or, if I add the partial modifier : "Partial declarations of 'App' must not specify different base classes"

Below you can find the code from a new Xamarin Forms project in Visual Studio 2019 with latest Updates and the list of errors that I cannot solve. Is the latest version of Xamarin Forms supposed to work with Caliburn .Micro or should I use a different Framework? I worked with WPF and CM for a long time and really liked it.

Thank you very much for your efforts and every help would be greatly appreciated. Maybe you could have a look at my problem or point me to a newer documantation that I was not able to find yet.

using Xamarin.Forms; using Xamarin.Forms.Xaml; using TestCM.Services; using TestCM.Views;

TestCM_Errorlist

using Caliburn.Micro.Xamarin.Forms; using Caliburn.Micro; using TestCM.ViewModels;

namespace TestCM { public partial class App : FormsApplication { private readonly SimpleContainer container;

    public App(SimpleContainer container)
    {
        Initialize();

        this.container = container;

        container.PerRequest<HomeViewModel>();

        DisplayRootView<HomeView>();
    }

    protected override void PrepareViewFirst(NavigationPage navigationPage)
    {
        container.Instance<INavigationService>(new NavigationPageAdapter(navigationPage));
    }
}

}

<?xml version="1.0" encoding="utf-8" ?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="TestCM.App">

[Activity(Label = "Setup.Forms", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(IoC.Get()); } } using System; using System.Collections.Generic; using System.Reflection; using Android.App; using Android.Runtime; using Caliburn.Micro; using TestCM; using TestCM.ViewModels; namespace TestCM.Forms.Droid { [Application] public class Application : CaliburnApplication { private SimpleContainer container; public Application(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { } public override void OnCreate() { base.OnCreate(); Initialize(); } protected override void Configure() { container = new SimpleContainer(); container.Instance(container); container.Singleton(); } protected override IEnumerable SelectAssemblies() { return new[] { GetType().Assembly, typeof (HomeViewModel).Assembly }; } protected override void BuildUp(object instance) { container.BuildUp(instance); } protected override IEnumerable GetAllInstances(Type service) { return container.GetAllInstances(service); } protected override object GetInstance(Type service, string key) { return container.GetInstance(service, key); } } }
nigel-sampson commented 4 years ago

I'd recommend looking at the features examples as they're using more modern versions.

asWorks commented 4 years ago

Thank you for your quick response - this is very much appreciated. I looked into the features examples but just find a Features.Forms part which seems to represent Xamarin.Forms. In this I find a FormsApp.cs file. It again shows me that the App class is inherited from FormsApplication witout a partial modifier which is exactly what the setup samples show me and which again brings me the error :

"Missing partial modifier on declaration of type 'App'; another partial declaration of this type exists TestCM" or, if I add the partial modifier : "Partial declarations of 'App' must not specify different base classes"

Is is genrally right the Camliburn.Micro for Xamarin.Forms Version 4.X is initialized in the App file codebehind and not in a Bootstrrapper class as shown in the setup examples and in the features examples?

And if - how do I solve this error.

I am really stuck and confused and not even sure if Caliburn.Micro supports Xamarin.Forms at all and is keeping pace with the currently quick develpment of Xamarin.Forms.

If you need any more specific information about my problem please let me know.

Thank you very much for your efforts.

asWorks

nigel-sampson commented 4 years ago

It sounds like there's some code being generated somewhere that has a partial and an inherited class somewhere.

asWorks commented 4 years ago

Hello Nigel, thank you again for your response. Well as far as I know every Xamarin.Forms App uses a Page called App.xaml which has a partial Codebehind class call App.cls that inherits from Application. Searching the documentation for caliburn.micro I found that I should let the App.cls inherit from a Caliburn.Micro Class which then brings me the afore mentioned error.

And I cannot find any documentation that tells me how to set up Xamarin.Forms with Caliburn.Micro and I don´t seem to be able to get any answer to that question.

I really appreciate your work for this great framework and I understand that documentation is the least satisfying part of this work.

It is really a pity though because in this state it makes it impossible for a beginner to make any use of it - even to set it up seems to be impossible.

So it seems I will have to look for another framework which I really regret because I used CM with WPF and I really liked it.

Kind regards

A. Stoever

nigel-sampson commented 4 years ago

Ah, it looks like the templates have moved on in XF since I last built them (its been a few years).

If you update App.xaml root node to FormsApplication it'll change the generated codes inherited model.

asWorks commented 4 years ago

Hmm, sorry to have to say - I don´t understand what you mean. Could you be a little more specific? Where shall I update the root node of App.Xaml? Please understand that I am a newbie.

nigel-sampson commented 4 years ago

When you have a xaml file the root node defines the class it inherits from. So the following xaml means the the generated code ends up in public partial class App : Application.

<Application>

Changing this to <cm:FormsApplication> lets you tweak it to public partial class App : FormsApplication which means your non generated code only has to be public partial class App.

If you look at the UWP code you can see it doing it there.

asWorks commented 4 years ago

Thank you very much for your patience. 🥇 This finally did the trick and I have it up and running. 👍

Kind regards

A. Stoever

asWorks commented 4 years ago

Closing it because the problem is solved - thanks a lot again.