小编Sim*_*son的帖子

自定义控件不显示

我正在尝试学习如何设计 Wpf .NET Core 自定义控件。我一直在严格关注 YouTube 视频,但在运行该解决方案时,该控件未显示在主窗口上。有人能看到我做错了什么吗?

主窗口.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>
Run Code Online (Sandbox Code Playgroud)

/主题/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>
Run Code Online (Sandbox Code Playgroud)

/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)));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

/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 …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf controls .net-core

2
推荐指数
1
解决办法
622
查看次数

标签 统计

.net ×1

.net-core ×1

c# ×1

controls ×1

wpf ×1