本文介绍了创建Windows Store应用页面,而XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个C#/ XAML的Windows商店应用,并想从C#创建完全和导航并显示一个页面。这可能吗?很显然,我可以从页面继承,但是,试图浏览到一个页面导出一个没有XAML的时候,我得到一个System.TypeLoadException ......找不到Windows运行时类型'Windows.Foundation'。



我的想法是,这应该是可能的,因为XAML转换成一个CLR部分类定义,因此没有理由一个人不能做在C#中的一切。但很明显我缺少某种框架的要求。



建议?



现在所有的我都派生页

 使用Windows.UI.Xaml.Controls; 

命名App1的{
公共类第2页:第{
公共第二页(){}
}
}

和这里是完全例外:

 找不到Windows运行时类型'Windows.Foundation。在System.StubHelpers.WinRTTypeNameConverter.GetTypeFromWinRTTypeName 
(字符串的typeName,布尔和放大器; isPrimitive)
在System.StubHelpers.SystemTypeMarshaler.ConvertToManaged(TypeNameNative * pNativeType,类型和放大器; managedType)
在Windows.UI。 Xaml.Controls.Frame.Navigate(类型sourcePageType)
在App1.MainPage< P2> d__2.MoveNext()


解决方案

我可以举个例子一个简单的页面

 使用系统; 
使用Windows.ApplicationModel.Activati​​on;
使用Windows.UI;
使用Windows.UI.Xaml;使用Windows.UI.Xaml.Controls
;
使用Windows.UI.Xaml.Media;
命名空间MyApp的
{
类节目
{
公共静态无效的主要(字串[] args)
{
Application.Start(( p)=>新建MyApp的());
}
}
类MyApp的:Windows.UI.Xaml.Application
{
公共MyApp的(){}
保护覆盖无效OnLaunched(LaunchActivatedEventArgs ARGS )
{
变种layoutRoot =新的网格(){BACKGROUND =新的SolidColorBrush(Colors.Blue)};
layoutRoot.Children.Add(新按钮(){CONTENT =你好!});
Window.Current.Content = layoutRoot;
Window.Current.Activate();
}
}
}



替换为正确的这些路径项目编译时:

  C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc。 EXE /目标:appcontainerexe /r:\"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll/ R:C:\Windows\Microsoft。 NET\Framework\v4.0.30319\System.Runtime.dll/r:\"C:\Windows\System32\WinMetadata\windows.applicationmodel.winmd/ R:C:\Windows\\ \\System32\WinMetadata\windows.ui.winmd/r:\"C:\Windows\System32\WinMetadata\windows.ui.xaml.winmd/ R:C:\Windows\System32 \WinMetadata\windows.media.winmdMyApp.cs 


I am writing a C#/XAML Windows Store app and would like to create and navigate to and display a Page entirely from C#. Is this possible? Obviously I can inherit from Page, but, when attempting to Navigate to a derived Page that has no XAML, I get a System.TypeLoadException... "Could not find Windows Runtime type 'Windows.Foundation'".

My thought was this should be possible since XAML translates into a CLR partial class definition, so there's no reason one couldn't do everything in C#. But obviously I missing some sort of framework requirement.

Suggestions?

Right now all I have for the derived Page is

using Windows.UI.Xaml.Controls;

namespace App1 {
    public class Page2 : Page {
        public Page2 () { }
    }
}

And here is the full exception:

Could not find Windows Runtime type '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)
at App1.MainPage.<P2>d__2.MoveNext()
解决方案

I can give an example for a simple page

using System;
using Windows.ApplicationModel.Activation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace MyApp
{
    class Program
    {
        public static void Main (string[] args)
        {
            Application.Start((p) => new MyApp());
        }
    }
    class MyApp : Windows.UI.Xaml.Application
    {
        public MyApp(){}
        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            var layoutRoot = new Grid() { Background = new SolidColorBrush(Colors.Blue) };
            layoutRoot.Children.Add(new Button() { Content = "Hello!" });
            Window.Current.Content = layoutRoot;
            Window.Current.Activate();
        }
    }
}

Replace these paths with the correct items when compiling:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:appcontainerexe /r:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll" /r:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.dll" /r:"C:\Windows\System32\WinMetadata\windows.applicationmodel.winmd" /r:"C:\Windows\System32\WinMetadata\windows.ui.winmd" /r:"C:\Windows\System32\WinMetadata\windows.ui.xaml.winmd" /r:"C:\Windows\System32\WinMetadata\windows.media.winmd" MyApp.cs

这篇关于创建Windows Store应用页面,而XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:17