XAML x:Bind 有一些奇怪的行為,并試圖系結到一個可為空的 int:
系結定義:
public ObservableCollection<ListObject> List { get; set; }
public class ListObject
{
public int? ID { get; set; }
public string Name { get; set; }
}
XAML
<ListView ItemsSource="{x:Bind ViewModel.List}"
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValue="{x:Bind ViewModel.SelectedID, Mode=TwoWay}"/>
以下作品沒有問題:
private int _selectedID;
public int SelectedID
{
get { return _selectedID; }
set { _selectedID = value; }
}
這不起作用,并且給了我一個無效的 int 物件?鑄造例外:
private int? _selectedID;
public int? SelectedID
{
get { return _selectedID; }
set { _selectedID = value; }
}
為什么 XAML 系結在常規 int 上起作用,而可為空的 int 會導致問題?
更新
在進一步研究了這個問題之后,g.cs 檔案似乎正確地轉換了 int,但是如果系結目標是一個可為空的結構,則不會自動生成轉換。
用于 int 系結目標的 g.cs 檔案:
private void UpdateTwoWay_25_SelectedItem()
{
if (this.initialized)
{
if (this.dataRoot != null)
{
if (this.dataRoot.ViewModel != null)
{
this.dataRoot.ViewModel.SelectedID = (global::System.Int32)this.obj25.SelectedItem;
}
}
}
}
這是可空結構上的 g.cs 檔案的樣子:
private void UpdateTwoWay_25_SelectedItem()
{
if (this.initialized)
{
if (this.dataRoot != null)
{
if (this.dataRoot.ViewModel != null)
{
// This line is the issue
this.dataRoot.ViewModel.SelectedID = this.obj25.SelectedItem;
}
}
}
}
手動更新 g.cs 檔案使系結正常作業。我不知道這是否是新的 x:bind、WinUI 3 或 Windows App SDK 的問題。
無論哪種方式,我在 Microsoft-UI-XAML git 中創建了一個錯誤: 問題 6558
uj5u.com熱心網友回復:
x:Bind當前不能null正確處理值。正如2020 年的這個 GitHub 問題所述,他們“將考慮修復這個帖子WinUI 3.0”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407699.html
標籤:
下一篇:如何添加按鈕組而不是單選按鈕
