C# 为什么可以';自定义列表视图是否有它';谁有自己的xaml文件?
为什么我的自定义ListView不能有自己的xaml文件?我有一个自定义按钮,它与一个xaml文件一起工作,没有任何问题,但不是我的ListView。我想使用这种方法(而不是被迫创建放置在Generic.xaml文件中的样式)的主要原因是,我想利用Resources元素并将与listview相关的所有资源放置在xaml文件中:C# 为什么可以';自定义列表视图是否有它';谁有自己的xaml文件?,c#,xaml,uwp,uwp-xaml,C#,Xaml,Uwp,Uwp Xaml,为什么我的自定义ListView不能有自己的xaml文件?我有一个自定义按钮,它与一个xaml文件一起工作,没有任何问题,但不是我的ListView。我想使用这种方法(而不是被迫创建放置在Generic.xaml文件中的样式)的主要原因是,我想利用Resources元素并将与listview相关的所有资源放置在xaml文件中: public sealed partial class MyListView : ListView { public MyListView() {
public sealed partial class MyListView : ListView
{
public MyListView()
{
this.InitializeComponent();
}
}
<ListView
x:Class="App1.MyListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<ListView.Resources>
<!-- I would like to place all related resources here instead of having
them placed in external locations, and then have to open different files to find them. -->
</ListView.Resources>
</ListView>
下面是关联的xaml文件:
public sealed partial class MyListView : ListView
{
public MyListView()
{
this.InitializeComponent();
}
}
<ListView
x:Class="App1.MyListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<ListView.Resources>
<!-- I would like to place all related resources here instead of having
them placed in external locations, and then have to open different files to find them. -->
</ListView.Resources>
</ListView>
虽然我希望这也能起作用,但似乎这个问题已经存在并正在解决
我认为问题在于,分配编译器无法为控件的
Items
属性生成有效的分配,它正试图从元素的内容构造该属性。即使元素立即关闭,这似乎也是一个问题。虽然我希望这也能起作用,但这个问题似乎是存在和存在的
我认为问题在于,分配编译器无法为控件的
Items
属性生成有效的分配,它正试图从元素的内容构造该属性。即使元素立即关闭,这似乎也是一个问题。为什么不将资源放在页面或ListView中,而不是派生您自己的控件
<Page
x:Class="ListViewResources.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ListViewResources"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<!-- Place all related resources here instead of having them placed in external locations, and then have to open different files to find them. -->
</Page.Resources>
<ListView x:Name="MyListView">
<ListView.Resources>
<!-- Or place related resources here -->
</ListView.Resources>
</ListView>
</Page>
为什么不将资源放在页面或ListView中,而不是派生您自己的控件
<Page
x:Class="ListViewResources.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ListViewResources"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<!-- Place all related resources here instead of having them placed in external locations, and then have to open different files to find them. -->
</Page.Resources>
<ListView x:Name="MyListView">
<ListView.Resources>
<!-- Or place related resources here -->
</ListView.Resources>
</ListView>
</Page>
尝试这样做时会发生什么?您是否收到错误?我收到“XAML解析失败”错误。错误似乎来自InitializeComponent()方法。需要查看更多的实现,以对所描述的行为进行故障排除。而不是为列表创建xaml文件,您可以创建一个名为MyCustomList\u Styles.xaml的ResourceDictionary
,并将所有与相关的列表
样式放在那里。当您尝试这样做时会发生什么?您是否收到错误?我收到“XAML解析失败”错误。错误似乎来自InitializeComponent()方法。需要查看更多的实现,以对所描述的行为进行故障排除。而不是为列表创建xaml文件,您可以创建一个名为MyCustomList\u Styles.xaml的ResourceDictionary
,并将所有列表
相关的样式
放在那里。感谢您的回复。如果你愿意的话,你的回答会让我想出我自己的小“黑客”。我所做的是将对InitializeComponent()的调用封装在try-catch块中。所以我想它至少允许我现在向xaml文件添加资源。你介意自己测试一下,看看是否得到同样的结果吗?如果这种方法一直有效,那么我肯定会将其应用到我的工作流程中,直到Microsoft提供“修复”。我这么说是因为人们说在WPF中拥有一个xaml文件是没有问题的。如果您通过查看InitializeComponent()方法的定义来检查它,您将看到Windows.UI.xaml.Application.LoadComponent()方法导致了这个问题。故事的寓意:只要LoadComponent()方法没有被Microsoft更改,在加载导致错误的任何原因之前阻止加载xaml文件的资源,您就可以使用“try-catch-hack”。感谢您的回复。如果你愿意的话,你的回答会让我想出我自己的小“黑客”。我所做的是将对InitializeComponent()的调用封装在try-catch块中。所以我想它至少允许我现在向xaml文件添加资源。你介意自己测试一下,看看是否得到同样的结果吗?如果这种方法一直有效,那么我肯定会将其应用到我的工作流程中,直到Microsoft提供“修复”。我这么说是因为人们说在WPF中拥有一个xaml文件是没有问题的。如果您通过查看InitializeComponent()方法的定义来检查它,您将看到Windows.UI.xaml.Application.LoadComponent()方法导致了这个问题。这个故事的寓意是:只要LoadComponent()方法没有被Microsoft更改,从而阻止它在加载导致错误的任何原因之前加载xaml文件的资源,就可以使用“try-catch-hack”。