简体   繁体   中英

WinPhone 8.1 project with Xamarin.Forms and Caliburn.Micro failes to create view

I have a Xamarin.Forms project with a WinPhone 8.1 project that uses Caliburn.Micro .

This is my App.xaml:

<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" />

This is my App.xaml.cs:

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;
  }
}

}

These are the NuGet packages:

在此输入图像描述

It compiles, but when I try to run it, I get the following exception at DisplayRootView :

Could not find Windows Runtime type 'Windows.Foundation'.":"Windows.Foundation

   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)

Can you tell me what I should do about this?

UPDATE:

The root of the problem lies in this part of the code:

  private global::MyProject.WinPhone.MyProject_WinPhone_XamlTypeInfo.XamlTypeInfoProvider _provider;

    public global::Windows.UI.Xaml.Markup.IXamlType GetXamlType(global::System.Type type)
    {
        if(_provider == null)
        {
            _provider = new global::MyProject.WinPhone.MyProject_WinPhone_XamlTypeInfo.XamlTypeInfoProvider();
        }
        return _provider.GetXamlTypeByType(type);
    }

    public global::Windows.UI.Xaml.Markup.IXamlType GetXamlType(string fullName)
    {
        if(_provider == null)
        {
            _provider = new global::MyProject.WinPhone.MyProject_WinPhone_XamlTypeInfo.XamlTypeInfoProvider();
        }
        return _provider.GetXamlTypeByName(fullName);
    }

    public global::Windows.UI.Xaml.Markup.XmlnsDefinition[] GetXmlnsDefinitions()
    {
        return new global::Windows.UI.Xaml.Markup.XmlnsDefinition[0];
    }
}
}

namespace MyProject.WinPhone.MyProject_WinPhone_XamlTypeInfo
{

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks", "4.0.0.0")]    
internal partial class XamlTypeInfoProvider
{
    public global::Windows.UI.Xaml.Markup.IXamlType GetXamlTypeByType(global::System.Type type)
    {
        global::Windows.UI.Xaml.Markup.IXamlType xamlType;
        if (_xamlTypeCacheByType.TryGetValue(type, out xamlType))
        {
            return xamlType;
        }
        int typeIndex = LookupTypeIndexByType(type);
        if(typeIndex != -1)
        {
            xamlType = CreateXamlType(typeIndex);
        }
        var userXamlType = xamlType as global::MyProject.WinPhone.MyProject_WinPhone_XamlTypeInfo.XamlUserType;
        if(xamlType == null || (userXamlType != null && userXamlType.IsReturnTypeStub && !userXamlType.IsLocalType))
        {
            global::Windows.UI.Xaml.Markup.IXamlType libXamlType = CheckOtherMetadataProvidersForType(type);
            if (libXamlType != null)
            {
                if(libXamlType.IsConstructible || xamlType == null)
                {
                    xamlType = libXamlType;
                }
            }
        }
        if (xamlType != null)
        {
            _xamlTypeCacheByName.Add(xamlType.FullName, xamlType);
            _xamlTypeCacheByType.Add(xamlType.UnderlyingType, xamlType);
        }
        return xamlType;
    }

It seems that my incoming MainView cannot be converted into an appropriate XAML type, as GetXamlTypeByType returns null.

在此输入图像描述

在此输入图像描述

I seems that the Xamarin.Forms view is not compatible with the WinPhone world...well, Xamarin.Forms should be about abstract views if I recall correctly.

What should I do now?

After I carefully read in the Xamarin Forms tutorial how a Windows Phone 8.1 application is added, and modified accordingly, everything worked as expected.

In my case, the MainPage.xaml.cs has its original code as I didn't change it according to the tutorial.

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

As Caliburn.Micro uses the Xamarin infrastructure, these steps are required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM