none
怎么样在多线程里设置Run里的Text Property RRS feed

  • 问题

  • 你好,我有这样的一个问题:我想在多线程里去设置Run里的Text Property,却发现Run里的没有提供Text的DependencyProperty,请问有什么办法设置呢,下面是我的代码:

    XAML里面的代码:

    <TextBlock VerticalAlignment="Center" Margin="2">
                    <Run>正在添加图片:</Run>
                    <Run Foreground="Red" Name="AddPhotosNumber" >0</Run>
                    <Run>/</Run>
                    <Run Foreground="Green" Name="TotalPhotosNumber" >0</Run>
                    <Run >...</Run>
    </TextBlock>

    CS里面的代码,多线程设置上面加粗的run里面的Text属性

    this.AddPhotosNumber.Dispatcher.Invoke(new Action<DependencyProperty, object>(AddPhotosNumber.SetValue), DispatcherPriority.Background, Run.TextProperty , index。ToString());

    却发现这里面没有提供Text的DependencyProperty,请问怎么处理?

    谢谢
    2011年4月6日 9:06

答案

  • Hi, Hubert

    不知道下面的实例满不满足你的要求

    Xaml

     

    <Window x:Class="WpfApplication1.RunWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="RunWindow" Height="300" Width="300">
     <Grid>
      <TextBlock x:Name="m_addPhoto" VerticalAlignment="Center" Margin="2">
        <Run x:Name="m_label">正在添加图片:</Run>
        <Run Foreground="Red" Name="AddPhotosNumber" >0</Run>
        <Run>/</Run>
        <Run Foreground="Green" Name="TotalPhotosNumber" >0</Run>
        <Run x:Name="m_state" Text="{Binding PhotoNumbers}"/>
      </TextBlock>
      <Button Content="Add Photo" Width="100" Height="50" Click="Button_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
     </Grid>
    </Window>
    

     

    后置代码

    /// <summary>
      /// RunWindow.xaml 的交互逻辑
      /// </summary>
      public partial class RunWindow : Window
      {
        private int _totalPhotos = 10;
    
        public RunWindow()
        {
          InitializeComponent();
          TotalPhotosNumber.Text = _totalPhotos.ToString();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
          BackgroundWorker backgroundWorker = new BackgroundWorker();
          backgroundWorker.DoWork += new DoWorkEventHandler(OnWork);
    
          backgroundWorker.RunWorkerAsync();
    
          backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((o, args) =>
          {
            m_label.Text = "上传完成:";
            m_state.Text = string.Empty;
          });
        }
    
        private void OnWork(object sender, DoWorkEventArgs e)
        {
          for (int i = 1; i <= _totalPhotos; i++)
          {
            m_addPhoto.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
            {
              AddPhotosNumber.Text = i.ToString();
            }));
    
            Thread.Sleep(500);
          }
        }
      }

    这个是在4.0 测试的. 在4.0上我发现Run的Text属性已经是DependencyProperty了... 


    just another day.
    2011年4月7日 11:26

全部回复

  • 这个问题怎么没人理睬啊,请问哪位达人有没有好一点的解决方案?非常感谢!
    2011年4月7日 5:56
  • 没看到。

    根据你的描述,我觉得你就是想加粗Run里面的Text内容。

    如果是这样,你可以设置“FontWeight”属性,比如:

    AddPhotosNumber.FontWeight = FontWeights.Bold;

    就可以加粗你名字为“AddPhotosNumber”中的内容。

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年4月7日 8:09
    版主
  • 不好意思,可能我的描述不清楚,我不是改变字体,我是实时改变那个run里的Text的内容(这个实时改变的过程在我的程序里是例外一个线程),所以我想在多线程里面去改变,但发现run的DependencyProperty里面没有Text属性?所以我想有没有其他的方法去实现?
    2011年4月7日 8:52
  • 我没有进行测试,不过我觉得你可以使用绑定去完成你想要实现的功能,你可以参考:

    http://blogs.msdn.com/b/text/archive/2009/09/01/bindable-run.aspx

    这样可以完成动态的改变。

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年4月7日 10:06
    版主
  • Hi, Hubert

    不知道下面的实例满不满足你的要求

    Xaml

     

    <Window x:Class="WpfApplication1.RunWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="RunWindow" Height="300" Width="300">
     <Grid>
      <TextBlock x:Name="m_addPhoto" VerticalAlignment="Center" Margin="2">
        <Run x:Name="m_label">正在添加图片:</Run>
        <Run Foreground="Red" Name="AddPhotosNumber" >0</Run>
        <Run>/</Run>
        <Run Foreground="Green" Name="TotalPhotosNumber" >0</Run>
        <Run x:Name="m_state" Text="{Binding PhotoNumbers}"/>
      </TextBlock>
      <Button Content="Add Photo" Width="100" Height="50" Click="Button_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
     </Grid>
    </Window>
    

     

    后置代码

    /// <summary>
      /// RunWindow.xaml 的交互逻辑
      /// </summary>
      public partial class RunWindow : Window
      {
        private int _totalPhotos = 10;
    
        public RunWindow()
        {
          InitializeComponent();
          TotalPhotosNumber.Text = _totalPhotos.ToString();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
          BackgroundWorker backgroundWorker = new BackgroundWorker();
          backgroundWorker.DoWork += new DoWorkEventHandler(OnWork);
    
          backgroundWorker.RunWorkerAsync();
    
          backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((o, args) =>
          {
            m_label.Text = "上传完成:";
            m_state.Text = string.Empty;
          });
        }
    
        private void OnWork(object sender, DoWorkEventArgs e)
        {
          for (int i = 1; i <= _totalPhotos; i++)
          {
            m_addPhoto.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
            {
              AddPhotosNumber.Text = i.ToString();
            }));
    
            Thread.Sleep(500);
          }
        }
      }

    这个是在4.0 测试的. 在4.0上我发现Run的Text属性已经是DependencyProperty了... 


    just another day.
    2011年4月7日 11:26
  • 谢谢,这样是可以的.是我想要的。
    2011年4月8日 2:20