使用Xamarin.Forms和Caliburn.Micro的WinPhone 8.1项目很难创建视图

时间:2016-07-01 08:04:20

标签: c# xamarin.forms caliburn.micro xamarin.winphone

我有一个 Xamarin.Forms 项目,其中包含使用Caliburn.Micro的WinPhone 8.1项目。

这是我的App.xaml:

<ul id="menu">
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
    <li><a href="#">item 4</a></li>
</ul>

这是我的App.xaml.cs:

$(function() {
  $('#menu').slicknav();
});

}

这些是NuGet包:

enter image description here

它编译,但当我尝试运行它时,我在<caliburn:CaliburnApplication x:Class="MyProject.WinPhone.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:caliburn="using:Caliburn.Micro" xmlns:local="using:MyProject.WinPhone" /> 得到以下异常:

  

无法找到Windows运行时类型   'Windows.Foundation'。 “:” Windows.Foundation

public sealed partial class App : Caliburn.Micro.CaliburnApplication
{

private WinRTContainer container;

private TransitionCollection transitions;

public App()
{
  InitializeComponent();
}

protected override void Configure()
{
  container = new WinRTContainer();
  container.RegisterWinRTServices();

  //TODO: Register your view models at the container
  container.PerRequest<MainViewModel>();
}

protected override object GetInstance(Type service, string key)
{
  var instance = container.GetInstance(service, key);
  if (instance != null)
    return instance;
  throw new Exception("Could not locate any instances.");
}

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

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

protected override void PrepareViewFirst(Frame rootFrame)
{
  container.RegisterNavigationService(rootFrame);
}

protected override IEnumerable<Assembly> SelectAssemblies()
{
  return new[] { typeof(MainViewModel).GetTypeInfo().Assembly, typeof(Windows.Foundation.AsyncActionCompletedHandler).GetTypeInfo().Assembly };
}

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
  if (args.PreviousExecutionState == ApplicationExecutionState.Running)
    return;

  try
  {
    DisplayRootView<MainView>();
    //DisplayRootView<MainPage>();
    //DisplayRootViewFor<MainViewModel>();
  }
  catch (Exception ex)
  {
    var x = 8;
    throw;
  }
}

你能告诉我应该怎么做吗?

更新

问题的根源在于代码的这一部分:

DisplayRootView

似乎我的传入MainView无法转换为适当的XAML类型,因为 at System.StubHelpers.WinRTTypeNameConverter.GetTypeFromWinRTTypeName(String typeName, Boolean& isPrimitive) at System.StubHelpers.SystemTypeMarshaler.ConvertToManaged(TypeNameNative* pNativeType, Type& managedType) at Windows.UI.Xaml.Controls.Frame.Navigate(Type sourcePageType, Object parameter) at Caliburn.Micro.CaliburnApplication.DisplayRootView(Type viewType, Object paramter) at Caliburn.Micro.CaliburnApplication.DisplayRootView[T](Object parameter) at MyProject.WinPhone.App.OnLaunched(LaunchActivatedEventArgs args) 返回null。

enter image description here

enter image description here

我似乎Xamarin.Forms视图与WinPhone世界不兼容......好吧,如果我没记错的话,Xamarin.Forms应该是抽象视图。

我现在该怎么办?

1 个答案:

答案 0 :(得分:0)

在我仔细阅读Xamarin Forms tutorial如何添加Windows Phone 8.1应用程序并进行相应修改后,一切都按预期工作。

在我的情况下,MainPage.xaml.cs有其原始代码,因为我没有根据教程对其进行更改。

public sealed partial class MainPage  // REMOVE ": PhonePage"

由于Caliburn.Micro使用Xamarin基础架构,因此需要执行这些步骤。

相关问题