Dispatcher.Invoke() 를 이용하여 UI에 내용이 보이질 않습니다. 도움주세요

환경 Visual Studio 2022


MainWindow.xaml >>>>>


<Border BorderBrush="Gray" BorderThickness="1">
    <Grid>
        <!-- Title Bar -->
        <DockPanel Background="Gray" Height="30" VerticalAlignment="Top">
            <TextBlock Text="RichChat"
                       VerticalAlignment="Center"
                       Foreground="White"
                       FontSize="16"
                       Padding="10"
                       HorizontalAlignment="Left"/>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button x:Name="MinimizeButton" Content="_" Width="40" Height="30"/>
                <Button x:Name="MaximizeButton" Content="□" Width="40" Height="30"/>
                <Button x:Name="CloseButton" Content="X" Width="40" Height="30"/>
            </StackPanel>
        </DockPanel>

        <!-- Background Image -->
        <Image Source="{Binding BackgroundImage}" Stretch="Fill" />


        <!-- AdornerDecorator to Separate Transparency -->
        <!-- Content Grid with Transparent Background -->
        <Grid Margin="10,0,10,0" VerticalAlignment="Center" Height="423">
            <!-- Chat Background -->
            <Border x:Name="ChatBackground" Background="White" Margin="0,22,0,45">
                <!-- Scrollable Chat Panel -->
                <ScrollViewer x:Name="ChatScrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
                    <StackPanel x:Name="ChatPanel">
                        <!-- Chat messages will be added here programmatically -->
                    </StackPanel>
                </ScrollViewer>
            </Border>

            <!-- Transparency Slider -->
            <Slider x:Name="TransparencySlider"
                    Minimum="0.1"
                    Maximum="1.0"
                    Value="1.0"
                    VerticalAlignment="Bottom"
                    HorizontalAlignment="Center"
                    Width="
                    400"
                    Margin="0,0,0,10"/>
        </Grid>
    </Grid>
</Border>mal >>>

MainWindow.xaml.cs >>>>>


		// 테스트를 위해서 마우스 이동시 AddChatMessage() 를 호출하여 메인 윈도우에 출력되는 것 확인함.
        this.MouseDown += (sender, e) =>
        {
            if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
            {
                this.DragMove();
            }

            ChatMessage chatMessage = new ChatMessage();

            AddChatMessage();
        };
		
    private int index = 0;
    public void AddChatMessage()
    {
            TextBlock textBlock = new TextBlock
            {
                Text = "TEST MESAAGE   " + index++,
                Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("black")),
                Margin = new Thickness(5)
            };
            Console.WriteLine("@@@@@ >>>>> <<<<< -----------------------------------");

            ChatPanel.Children.Add(textBlock);
            ChatScrollViewer.ScrollToEnd();
    }

    public void AddChatMessage(ChatMessage message)
    {
        Console.WriteLine("13 >>> {0} {1} {2}", message.data.chat_data.message, message.data.chat_font, message.data.chat_color);
        //Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
        //{
        TextBlock textBlock = new TextBlock
        {
            Text = "================================",
            Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("black")),
            Margin = new Thickness(5)
        };

        ChatPanel.Children.Add(textBlock);
        ChatScrollViewer.ScrollToEnd();
    }

HTTPServer.cs >>>>>

	//	_mainWidnow = MainWindow 임....
                    //App.Current.Dispatcher.Invoke(() =>		// 해당 코드로도 UI에 텍스트 표시 안됨
                    _mainWindow.Dispatcher.Invoke(() =>         // 이 경우에도 UI에 텍스트 표시 안됨
                    {
                     //_MAINWINDOW.ADDMESSAGETOCHATWINDOW(MESSAGE.USER, MESSAGE.MESSAGE);
                        _mainWindow.AddChatMessage();   // 테스트를 위해 호출해 봄
                        _mainWindow.AddChatMessage(message);  // 

                        Console.WriteLine("20 >>> {0} {1} {2}", message.data.chat_data.message, message.data.chat_font, message.data.chat_color);
                    });

정확한 원인은 모르겠지만, 투명도 관련하여 처음부터 작은것부터 새로이 하니 정상 동작을 합니다.

감사합니다.

1 Like