自定义控件不显示
Custom Controls not displaying
我正在尝试学习如何设计 Wpf .NET Core 自定义控件。我一直在关注 YouTube 视频,但当涉及到 运行 解决方案时,控件未显示在主 window 上。有人能看出我做错了什么吗?
MainWindow.xaml
<Window x:Class="AnalogClock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AnalogClock"
xmlns:custom="clr-namespace:AnalogClock.CustomControls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Label Content="Test"/>
<StackPanel>
<custom:AnalogClock/>
</StackPanel>
</Grid>
</Window>
/Themes/Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AnalogClock.Themes">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/CustomControls/AnalogClockStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
/CustomControls/AnalogClock.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace AnalogClock.CustomControls
{
class AnalogClock : Control
{
static AnalogClock()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AnalogClock), new FrameworkPropertyMetadata(typeof(AnalogClock)));
}
}
}
/CustomControls/AnalogClockStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AnalogClock.CustomControls">
<Style TargetType="local:AnalogClock">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:AnalogClock">
<Grid>
<Line x:Name="PART_HourHand"
Stroke="Black"
StrokeThickness="2"
VerticalAlignment="Center"
HorizontalAlignment="Center"
X1="0"
X2="-75"/>
<Line x:Name="PART_MinuteHand"
Stroke="Black"
StrokeThickness="2"
VerticalAlignment="Center"
HorizontalAlignment="Center"
X1="0"
X2="-100"/>
<Line x:Name="PART_SecondHand"
Stroke="Red"
StrokeThickness="2"
VerticalAlignment="Center"
HorizontalAlignment="Center"
X1="0"
X2="-100"/>
<Ellipse x:Name="PART_Border"
Stroke="Black"
Fill="Black"
StrokeThickness="2"
Width="210"
Height="210"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
将以下属性添加到项目中的任何 class 文件,例如添加到定义了 AnalogClock
class 的文件:
[assembly: System.Windows.ThemeInfo(
System.Windows.ResourceDictionaryLocation.None,
System.Windows.ResourceDictionaryLocation.SourceAssembly
)]
与 .NET Framework 项目不同,.NET Core 项目默认不包含包含此属性的 AssemblyInfo.cs
文件。需要找到通用资源字典。
我正在尝试学习如何设计 Wpf .NET Core 自定义控件。我一直在关注 YouTube 视频,但当涉及到 运行 解决方案时,控件未显示在主 window 上。有人能看出我做错了什么吗?
MainWindow.xaml
<Window x:Class="AnalogClock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AnalogClock"
xmlns:custom="clr-namespace:AnalogClock.CustomControls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Label Content="Test"/>
<StackPanel>
<custom:AnalogClock/>
</StackPanel>
</Grid>
</Window>
/Themes/Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AnalogClock.Themes">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/CustomControls/AnalogClockStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
/CustomControls/AnalogClock.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace AnalogClock.CustomControls
{
class AnalogClock : Control
{
static AnalogClock()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AnalogClock), new FrameworkPropertyMetadata(typeof(AnalogClock)));
}
}
}
/CustomControls/AnalogClockStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AnalogClock.CustomControls">
<Style TargetType="local:AnalogClock">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:AnalogClock">
<Grid>
<Line x:Name="PART_HourHand"
Stroke="Black"
StrokeThickness="2"
VerticalAlignment="Center"
HorizontalAlignment="Center"
X1="0"
X2="-75"/>
<Line x:Name="PART_MinuteHand"
Stroke="Black"
StrokeThickness="2"
VerticalAlignment="Center"
HorizontalAlignment="Center"
X1="0"
X2="-100"/>
<Line x:Name="PART_SecondHand"
Stroke="Red"
StrokeThickness="2"
VerticalAlignment="Center"
HorizontalAlignment="Center"
X1="0"
X2="-100"/>
<Ellipse x:Name="PART_Border"
Stroke="Black"
Fill="Black"
StrokeThickness="2"
Width="210"
Height="210"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
将以下属性添加到项目中的任何 class 文件,例如添加到定义了 AnalogClock
class 的文件:
[assembly: System.Windows.ThemeInfo(
System.Windows.ResourceDictionaryLocation.None,
System.Windows.ResourceDictionaryLocation.SourceAssembly
)]
与 .NET Framework 项目不同,.NET Core 项目默认不包含包含此属性的 AssemblyInfo.cs
文件。需要找到通用资源字典。