【问题标题】:Caliburn.Micro Xamarin.Forms ViewModel does not initializeCaliburn.Micro Xamarin.Forms ViewModel 未初始化
【发布时间】:2017-05-07 06:42:16
【问题描述】:

我创建了一个新的 Xamarin.Forms 项目,但在初始化视图模型时遇到了问题。什么都没发生。我正在使用来自Features 的样本。我将示例代码修改为以下内容:

public FormsApp(SimpleContainer container)
    {
       //...

        DisplayRootView<ConductorView>();
    } 

这按预期工作,但在我的项目中它根本不起作用。

我使用的是 .NET Standard 1.5(不确定这是否会导致问题)。无论如何,这是我的代码

App.cs:

using System;
using Caliburn.Micro;
using Caliburn.Micro.Xamarin.Forms;
using UniversalSqlManager.ViewModels;
using Xamarin.Forms;
using INavigationService = Caliburn.Micro.Xamarin.Forms.INavigationService;
using UniversalSqlManager.Views;
namespace UniversalSqlManager
{
    public class App : FormsApplication
    {
        private readonly SimpleContainer container;

        public App(SimpleContainer container)
        {
            this.container = container;

            container
                .PerRequest<ShellViewModel>();

            this.DisplayRootView<ShellView>();
        }

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

ShellView.xaml:

    <?xml version="1.0" encoding="utf-8"?>
    <TabbedPage 
        xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        xmlns:local="clr-namespace:UniversalSqlManager" 
        x:Class="UniversalSqlManager.Views.ShellView"
        xmlns:cm="clr-namespace:Caliburn.Micro.Xamarin.Forms;assembly=Caliburn.Micro.Platform.Xamarin.Forms"
        ItemsSource="{Binding Items}"
        SelectedItem="{Binding ActiveItem, Mode=TwoWay}"
        Title="Universal SQL Manager">
        <TabbedPage.ItemTemplate>
            <DataTemplate>
                <ContentPage Title="{Binding DisplayName}" cm:View.Model="{Binding}" />
            </DataTemplate>
        </TabbedPage.ItemTemplate>
    </TabbedPage>

ShellView.xaml.cs:

    using Xamarin.Forms;

    namespace UniversalSqlManager.Views
    {
        public partial class ShellView
        {
            public ShellView()
            {
                InitializeComponent();
            }
        }
    }

ShellViewModel.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Caliburn.Micro;
    using Caliburn.Micro.Xamarin.Forms;
    using UniversalSqlManager.ViewModels.Interfaces;

    namespace UniversalSqlManager.ViewModels
    {
        public class ShellViewModel : Conductor<ITabItem>.Collection.OneActive
        {
            protected override void OnInitialize()
            {
                //both view models implement ITabItem (which has no methods) and inherit form Screen
                Items.Add(new ServersViewModel());
                Items.Add(new SettingsViewModel());

                ActivateItem(Items[0]);
            }
        }
    }

我将展示 iOS,因为这是我正在测试的第一个平台

AppDelegate.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Caliburn.Micro;

    using Foundation;
    using UIKit;

    namespace UniversalSqlManager.iOS
    {
        [Register(nameof(AppDelegate))]
        public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
        {
            private readonly CaliburnAppDelegate appDelegate = new CaliburnAppDelegate();

            public override bool FinishedLaunching(UIApplication app, NSDictionary options)
            {
                global::Xamarin.Forms.Forms.Init();

                LoadApplication(IoC.Get<App>());

                return base.FinishedLaunching(app, options);
            }
        }
    }

CaliburnAppdelegate.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Caliburn.Micro;
    using UniversalSqlManager.ViewModels.Interfaces;

    namespace UniversalSqlManager.iOS
    {
        public class CaliburnAppDelegate : CaliburnApplicationDelegate
        {
            private SimpleContainer container;

            public CaliburnAppDelegate()
            {
                Initialize();
            }

            protected override void Configure()
            {
                container = new SimpleContainer();
                container.Instance(container);
                container.Singleton<App>();
                container.Singleton<IEventAggregator, EventAggregator>();
            }

            protected override void BuildUp(object instance)
            {
                container.BuildUp(instance);
            }

            protected override IEnumerable<object> GetAllInstances(Type service)
            {
                return container.GetAllInstances(service);
            }

            protected override object GetInstance(Type service, string key)
            {
                return container.GetInstance(service, key);
            }
        }
    }

所以当我运行它时,会显示 ShellView UI,但 ShellViewModel 没有被初始化,我也没有得到任何选项卡。即使我切换到 SettingsView 或 ServersView,相应的视图模型也永远不会被初始化。我究竟做错了什么?这是我第一次在 Xamarin.Forms 上使用 Caliburn Micro。我过去使用 WPF 没有问题。只是文档令人困惑,因为看起来我们有这些示例可供参考,并且一些博客不如 WPF 文档详细。任何帮助,将不胜感激。如果这也有帮助,我可以发布我的 csproj 和 project.json,但我很犹豫是否要切换项目类型,因为设置它很痛苦。我想另一种选择是用 PCL 创建一个新项目,看看是否可行?没有想法了。任何帮助表示赞赏!

【问题讨论】:

    标签: c# xamarin.forms caliburn.micro


    【解决方案1】:

    所以问题是我没有告诉框架使用哪些程序集。功能示例不将库用于共享代码。所以我将它分别添加到 CaliburnAppDelegate.cs 和 Application.cs 到 iOS 和 Android:

    protected override IEnumerable<Assembly> SelectAssemblies()
    {
        return this.GetAssemblies();
    }
    

    这是我在类库中创建的扩展:

    public static class Bootstrapper
    {
    //...
    
    public static IEnumerable<Assembly> GetAssemblies(this object o)
        {
            IEnumerable<Assembly> assemblies =
            new[]
            {
                o.GetType().GetTypeInfo().Assembly,
                typeof(ShellViewModel).GetTypeInfo().Assembly
            };
            return assemblies;
        }
    //...
    }
    

    这样就解决了问题。我希望这可以帮助其他在使用类库连接示例时遇到问题的人。顺便说一句,我在与设置示例进行比较时发现了这一点。

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      相关资源
      最近更新 更多