WPF [호출 스레드에서 이 객체에 액세스할 수 없습니다. 다른 스레드가 해당 객체를 소유하고 있기 때문입니다.]해결 방안 및 글꼴 색의 점차적인 변화를 어떻게 실현하는가

3474 단어
<Window x:Class="MyWPFApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="70" Width="250" Loaded="Window_Loaded">
    <Grid>
        <TextBlock Height="36" HorizontalAlignment="Left"  Name="gc" Text=" !" VerticalAlignment="Top" Width="230" FontSize="24">
                <TextBlock.Foreground>
                    <LinearGradientBrush> 
                            <GradientStop Color="Green"></GradientStop>
                            <GradientStop x:Name="gcc1"  Color="Green" Offset="0.3"></GradientStop>
                            <GradientStop x:Name="gcc2" Color="Blue" Offset="0.3"></GradientStop>
                            <GradientStop Color="Blue" Offset="1"></GradientStop> 
                    </LinearGradientBrush>
                </TextBlock.Foreground>
        </TextBlock>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; 

namespace MyWPFApp
{
    /// <summary>
    /// MainWindow.xaml  
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Timers.Timer t = new System.Timers.Timer(200);// Timer , 200 ;   
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);  // ; 
            t.AutoReset = true;// (false) (true);    
            t.Enabled = true;  // System.Timers.Timer.Elapsed ;  , start() true  
        }

        public void theout(object source, System.Timers.ElapsedEventArgs e)
        { 
        
        }
    }
}
의뢰한 사용
public void theout(object source, System.Timers.ElapsedEventArgs e)
        {
            this.gcc1.Dispatcher.Invoke(
               new Action(
                    delegate
                    {
                        if (this.gcc1.Offset < 1)
                        {
                            this.gcc1.Offset += 0.1;
                        }
                        else
                        {
                            this.gcc1.Offset = 0;
                        }
                    }
               )
         );
            this.gcc2.Dispatcher.Invoke(
                   new Action(
                        delegate
                        {
                            if (this.gcc2.Offset < 1)
                            {
                                this.gcc2.Offset += 0.1;
                            }
                            else
                            {
                                this.gcc2.Offset = 0;
                            }
                        }
                   )
             ); 
        }

좋은 웹페이지 즐겨찾기