WPF CustomControl 핫 리로드

Rider를 주로 쓰고 있고 Visual Studio는 보조로 쓰고 있습니다. UI를 좀 디테일하게 건드려야 해서 클로드 코드로 못하고 한땀한땀 작업중인데요 .xaml은 hot reload가 지원이 잘 안되고 특히나 CustomControl 스타일로 작업 할 때 핫 리로드가 안되서 색상하나, 테두리 색 하나 바뀌는 것 확인 하려고 해도 ctrl + shift + f5로 다시 시작 하고 있습니다.

Rider는 잘 안된다고 하여 Visual Studio 2022로 해봤는데도 핫 리로드 적용을 못하고 있습니다.

xaml 작업시 Hot Reload 적용 할 방법이 있을까요?

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:units="clr-namespace:MV1000.Support.UI.Units">
    
    <Style TargetType="{x:Type Border}" x:Key="GoldLine">
        <Setter Property="Background" Value="#00070E"></Setter>
        <Setter Property="BorderBrush" Value="#34291E"></Setter>
        <Setter Property="BorderThickness" Value="1 1 1 1"></Setter>
    </Style>

    <Style TargetType="{x:Type units:PadButton}">
        <Setter Property="Height" Value="38"></Setter>
        <Setter Property="Width" Value="165"></Setter>
        <Setter Property="Foreground" Value="#FFFFFF"></Setter>
        <Setter Property="Background" Value="Transparent"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type units:PadButton}">
                    <Grid Background="{TemplateBinding Background}">
                        <Border Style="{StaticResource GoldLine}" /> 
                        <TextBlock Text="{TemplateBinding Content}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

using System.Windows;
using System.Windows.Controls.Primitives;

namespace MV1000.Support.UI.Units;

public class PadButton : ToggleButton
{
    static PadButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(PadButton),
            new FrameworkPropertyMetadata(typeof(PadButton)));
    }
}

StyleSetterStaticResource로 참조된 경우 Hot Reload가 반영되지 않을 수 있습니다. StaticResource는 컴파일 타임에 한 번만 resolve되기 때문입니다. DynamicResource로 참조된 경우엔 비교적 잘 반영됩니다. 반면, ControlTemplate 내부 요소들은 비주얼 트리를 직접 구성하는 영역이기 때문에 Hot Reload가 잘 동작합니다.

Hot Reload를 실행하려면 상단의 불꽃 아이콘을 눌러 수동으로 적용할 수 있고, 설정에서 “저장 시 자동 적용” 옵션을 활성화하면 파일을 저장할 때마다 자동으로 반영되도록 할 수 있습니다.

정리하면, Setter 영역은 Hot Reload를 기대하지 않는 것이 좋고, UI 변경 작업은 ControlTemplate 내부에서 진행하는 것이 효율적입니다. 또한 개발 편의를 위해 저장 시 자동 적용 옵션을 켜두는 것을 권장합니다.


그리고 이것도..

자료출처

2개의 좋아요

자문자답 해봅니다.

직접은 안되고 우회 할 방법이 있습니다.

위와 같이 ResourceDectionary를 UserControl로 한번 감싸면 Preview를 볼 수 있습니다.

1개의 좋아요