Application.Resources

时间:2015-07-30 01:38:53

标签: xaml windows-runtime datatemplate windows-10 uwp

使用Visual Studio 2015(RTM)的通用Windows平台应用程序

我有一个DataTemplate用于我的应用程序的多个页面,所以我更喜欢写一次并从我需要的任何地方访问它。为了使其可以通过任何页面访问,我将其写在我的App.xaml <Application.Resources>中:

<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:viewmodels="using:MyApp.ViewModels"
RequestedTheme="Light">

<Application.Resources>
    <DataTemplate x:Key="DetailContentTemplate" x:DataType="viewmodels:DataViewModel"> 
    ...
    </DataTemplate>
</Application.Resources>

上面代码的DataTemplate部分在单个页面中工作得很好,但当然这意味着我必须多次复制并粘贴到其他页面,这是不高效的。但是,当我在App.xaml中使用DataTemplate时出现此错误:

XBF generation error code 0x09c4

我已经确定它来自x:DataType="viewmodels:DataViewModel"(没有这个,因此,没有任何绑定,代码工作得很好)。查找错误导致几乎没有。是否有一个方便的解决方法/解决方案,能够在通用Windows平台/ WinRT应用程序中重用带有绑定的DataTemplate,最好是在XAML中?

编辑:根据要求,App.xaml.cs的完整代码:

namespace MyApp
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Application
{
    /// <summary>
    /// Allows tracking page views, exceptions and other telemetry through the Microsoft Application Insights service.
    /// </summary>
    public static Microsoft.ApplicationInsights.TelemetryClient TelemetryClient;

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        TelemetryClient = new Microsoft.ApplicationInsights.TelemetryClient();

        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="e">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();
            // Set the default language
            rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

            rootFrame.NavigationFailed += OnNavigationFailed;

            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MasterDetailPage));
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        // Ensure the current window is active
        Window.Current.Activate();
    }

    /// <summary>
    /// Invoked when Navigation to a certain page fails
    /// </summary>
    /// <param name="sender">The Frame which failed navigation</param>
    /// <param name="e">Details about the navigation failure</param>
    void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
    {
        throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
    }

    /// <summary>
    /// Invoked when application execution is being suspended.  Application state is saved
    /// without knowing whether the application will be terminated or resumed with the contents
    /// of memory still intact.
    /// </summary>
    /// <param name="sender">The source of the suspend request.</param>
    /// <param name="e">Details about the suspend request.</param>
    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        //TODO: Save application state and stop any background activity
        deferral.Complete();
    }
}

}

1 个答案:

答案 0 :(得分:4)

您可以在20:10的最新build session找到一些解释 您基本上需要在XAML中创建资源字典并将类附加到它。需要此类文件以允许编译器生成其代码。

根据您必须执行的操作以及如何更改代码,您仍然可以使用“旧”{Binding}标记,该标记将像以前一样工作。

<Application.Resources>
    <DataTemplate x:Key="DetailContentTemplate"> 
        <TextBlock Text={Binding myValue} />
    </DataTemplate>
</Application.Resources>