Catel 与 Xamarin.Forms

Posted

技术标签:

【中文标题】Catel 与 Xamarin.Forms【英文标题】:Catel with Xamarin.Forms 【发布时间】:2017-08-28 12:34:14 【问题描述】:

我无法让 Xamarin.Forms 和 Catel 一起工作。

我看到了这个sample,但它似乎已经过时了。

当我通过 NuGet 下载 Catel 时,我遇到不同版本的不同问题:

v5.1:

编译时异常:

Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'Obsolete, Version=4.2.4.0, Culture=neutral, PublicKeyToken=1ca091877d12ca03'. Perhaps it doesn't exist in the Mono for android profile?

v5.0:

运行时异常(创建new App();时):

Could not load signature of Catel.MVVM.IViewModelFactory:CreateViewModel due to: Could not resolve type with token 01000061 (from typeref, class/assembly System.ComponentModel.IDataErrorInfo, Catel.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null) assembly:Catel.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null type:System.ComponentModel.IDataErrorInfo member:<none>

v4.5.*

使用ViewModelBase

创建应用程序对象时的运行时异常: System.TypeLoadException:无法使用令牌解析类型 01000097(来自 typeref、类/程序集 System.Reflection.BindingFlags、Catel.Core、Version=4.5.4.0、Culture=neutral、PublicKeyToken=null)

我对@9​​87654326@的实现:

好的,MainPage 加载,绑定工作,构造函数通过参数获得正确的服务,但是,例如,当我调用 '_uiVisualizerService.Show(subPageViewModel);'从命令,它抛出'InvalidCastException'。异常前 Catel 日志中的最后一行:Injecting properties into type 'SubPage' after construction。但是 SubPage 和 SubPageViewModel 绝对是空的。

详情:

---> System.InvalidCastException: Specified cast is not valid. at Catel.Services.UIVisualizerService+<ShowAsync>d__9.MoveNext () [0x00096] in C:\CI_WS\Ws\97969\Source\Catel\src\Catel.MVVM\Catel.MVVM.Xamarin.Forms\Services\UIVisualizerService.cs:151 at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <183b200ee49746d48fc781625979428e>:0 at System.Threading.Tasks.Task'1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in <183b200ee49746d48fc781625979428e>:0 at System.Threading.Tasks.Task'1[TResult].get_Result () [0x0000f] in <183b200ee49746d48fc781625979428e>:0 at Catel.Services.UIVisualizerService.Show (Catel.MVVM.IViewModel viewModel, System.EventHandler``1[TEventArgs] completedProc) [0x00000] in C:\CI_WS\Ws\97969\Source\Catel\src\Catel.MVVM\Catel.MVVM.Xamarin.Forms\Services\UIVisualizerService.cs:125 at XamarinFormsCatel.ViewModels.MainPageViewModel.OnGo () [0x00015] in C:\!Work\Test\XamarinFormsCatel\XamarinFormsCatel\XamarinFormsCatel\XamarinFormsCatel\ViewModels\MainPageViewModel.cs:34 at Xamarin.Forms.Command+<>c__DisplayClass3_0.<.ctor>b__0 (System.Object o) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Command.cs:73 at Xamarin.Forms.Command.Execute (System.Object parameter) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Command.cs:107 at Xamarin.Forms.Button.Xamarin.Forms.IButtonController.SendClicked () [0x0000a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Button.cs:121 at Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer+ButtonClickListener.OnClick (Android.Views.View v) [0x0000b] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\ButtonRenderer.cs:298 at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x0000f] in <71828dd8fb5f4508815e23d6996c45c2>:0 at (wrapper dynamic-method) System.Object:4c9f6786-8ab0-4bd8-bdb1-4cc7a3d7ddc7 (intptr,intptr,intptr) -> (Inner Exception #0) System.InvalidCastException: Specified cast is not valid. at Catel.Services.UIVisualizerService+<ShowAsync>d__9.MoveNext () [0x00096] in C:\CI_WS\Ws\97969\Source\Catel\src\Catel.MVVM\Catel.MVVM.Xamarin.Forms\Services\UIVisualizerService.cs:151 <---

我做什么:

android 项目中的默认Catel.MVVM.dll 引用从MonoAndroid 更改为portable-net45+wp8+win8+wpa81+MonoAndroid10+MonoTouch10+Xamarin.ios10+UAP10。如果不这样做,编译将失败。

MainPage.xaml

<catel:ContentPage ... xmlns:catel="clr-namespace:Catel.Windows.Controls;assembly=Catel.MVVM">

MainPageViewModel.cs

public class MainPageViewModel : ViewModelBase

App.cs

public class App : Catel.Xamarin.Forms.Application&lt;Views.MainPage&gt;

MainActivity.cs (Android)

public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity

    protected override void OnCreate( Bundle bundle )
    
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate( bundle );

        Xamarin.Forms.Forms.Init( this, bundle );

        var app = new App();
        LoadApplication( app );
    

Windows 7、64x。 Xamarin.Forms:2.3.4.267

非常感谢任何帮助。

【问题讨论】:

【参考方案1】:

团队正在努力改进对 Xamarin.Forms 的支持。目前没有例子。期望在接下来的几个月内最终确定对 Xamarin.Forms 的 v5 支持。

【讨论】:

什么都没发生,现在是 2020 年,仍然没有 Xamarin.Forms 的示例

以上是关于Catel 与 Xamarin.Forms的主要内容,如果未能解决你的问题,请参考以下文章

Catel 示例在 Visual Studio 中加载非常缓慢

DataWindowButton CanExecute 未触发,Catel 4.0

viewmodel + Catel 上的验证

Catel MVVM 结构

通过 Catel 命令使用参数

如何从 Catel 项目迁移到 Orchestra + MahApps? (.NET 4.0)