优化空引用

This commit is contained in:
2026-02-23 15:05:15 +08:00
parent 5a3a368a2e
commit 1d939d52ed
10 changed files with 9 additions and 51 deletions

View File

@@ -85,13 +85,13 @@ public class Cascader : Control
private string GetObjectDisplayText(object? item)
{
if (item == null) return "";
if (item == null) return string.Empty;
if (!string.IsNullOrEmpty(DisplayMemberPath))
{
var prop = item.GetType().GetProperty(DisplayMemberPath);
return prop?.GetValue(item)?.ToString() ?? "";
return prop?.GetValue(item)?.ToString() ?? string.Empty;
}
return item.ToString();
return item.ToString()!;
}
// 创建了完整的、非空的 CascadingPanel
@@ -130,7 +130,7 @@ public class Cascader : Control
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 0) return;
var selectedItem = e.AddedItems[0];
var selectedItem = e.AddedItems[0]!;
var currentMenu = sender as ItemsControl;
var parentPanel = CascadingPanel;
if (parentPanel == null) return;

View File

@@ -31,8 +31,7 @@ namespace Melskin.Controls
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_editableTextBox = GetTemplateChild("PART_EditableTextBox") as TextBox;
if (_editableTextBox != null)
if (GetTemplateChild("PART_EditableTextBox") is TextBox _editableTextBox)
{
_editableTextBox.TextChanged += OnEditableTextBoxTextChanged;
// 点击时全选
@@ -100,7 +99,7 @@ namespace Melskin.Controls
var prop = item.GetType().GetProperty(path);
if (prop != null) return prop.GetValue(item)?.ToString() ?? string.Empty;
}
return item.ToString();
return item.ToString()!;
}
}
}