WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变

< 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 >
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)
         {
         
         }
     }
}
委托的使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
                             }
                         }
                    )
              );
         }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值