gpt4 book ai didi

xaml - Application.Resources 中的 DataTemplate 出现 XBF 错误

转载 作者:行者123 更新时间:2023-12-01 00:50:14 24 4
gpt4 key购买 nike

带有 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" (没有这个,因此,没有任何绑定(bind),代码就可以正常工作)。查找错误几乎没有结果。是否有一种方便的解决方法/解决方案能够在通用 Windows 平台/WinRT 应用程序中重用具有绑定(bind)的 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();
}
}

}

最佳答案

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

根据您必须做什么以及如何更改代码,您仍然可以使用“旧的” {Binding} 标记,它可以像以前一样工作。

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

关于xaml - Application.Resources 中的 DataTemplate 出现 XBF 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31713873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com