在Windows8 Winrt中 高性能处理多个条件语句 用于实现自定义手势

it2022-05-06  5

http://blog.csdn.net/wangrenzhu2011/article/details/8578806 (转)

在winrt中 多点触控 控件的应用越来越多,例如 各种手势与 控件之间的互动,如常规的:放大缩小,滑动,旋转,轻扫等。。

但是如果我们需要自定义一些手势,那么需要大量的条件语句判断,严重的影响了性能,往往这些判断在ui 中计算,当触控点过多时会引起系统内响应过慢。

 

当然 常规的解决办法是将这些条件判断放置在多线程中处理。但是该方式治标不治本,条件过多时,代码会显得非常混乱 难以维护。。

下面我将介绍一种比较合理的方式处理该问题。

 

 

通过Predict定义多个条件,通过Action或者Func 来实现判断之后的效果。

Dictionary<Predicate<int>, Action> testDict = new Dictionary<Predicate<int>, Action>();

或者Dictionary<Predicate<int>, Task<Action>> testDict = new Dictionary<Predicate<int>, Task<Action>>();

Dictionary<Predicate<int>, Func<int>> testDict = new Dictionary<Predicate<int>, Func<int>>();

 

我们以第一个testDict 写一个sample验证一下。

 

[csharp]  view plain copy print ?   using System;  using System.Collections.Generic;  using System.Diagnostics;  using System.IO;  using System.Linq;  using System.Threading.Tasks;  using Windows.Foundation;  using Windows.Foundation.Collections;  using Windows.UI.Input;  using Windows.UI.Xaml;  using Windows.UI.Xaml.Controls;  using Windows.UI.Xaml.Controls.Primitives;  using Windows.UI.Xaml.Data;  using Windows.UI.Xaml.Input;  using Windows.UI.Xaml.Media;  using Windows.UI.Xaml.Navigation;    // “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍    namespace Test  {      /// <summary>      /// 可用于自身或导航至 Frame 内部的空白页。      /// </summary>      public sealed partial class MainPage : Page      {          public MainPage()          {              this.InitializeComponent();                this.PointerPressed += ((sender, e) =>              {                  var p = e.GetCurrentPoint(this);                  var currentPoint = p;                  var start = DateTime.Now.Millisecond;                  var x = Convert.ToInt32(p.Position.X);                  foreach (var item in testDict.                      Where(o =>                      {                          return Task<bool>.Run(() =>                          {                              return o.Key(x);                          }).Result;                      }))                  {                        item.Value();                    }                  var end = DateTime.Now.Millisecond;                  Debug.WriteLine(start);                  Debug.WriteLine(end);                  Debug.WriteLine("时差:" + (start - end).ToString());              });          }            Dictionary<Predicate<int>, Action> testDict = new Dictionary<Predicate<int>, Action>();          /// <summary>          /// 在此页将要在 Frame 中显示时进行调用。          /// </summary>          /// <param name="e">描述如何访问此页的事件数据。Parameter          /// 属性通常用于配置页。</param>          protected override void OnNavigatedTo(NavigationEventArgs e)          {              Predicate<int> pp = CheckPointer;              testDict.Add(CheckPointer, async () =>              {                  await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>                  {                      TextBlock tb = new TextBlock();                      tb.Text = "打印";                      lv.Items.Add(tb);                  });              });          }            public static bool CheckPointer(int p)          {              for (int i = 9999999; i > 0; i--)              {                  if (i == p)                  {                      return true;                  }              }              return false;          }                private void Button_Click_1(object sender, RoutedEventArgs e)          {            }      }  }  

 

 

 

[html]  view plain copy print ?   <Page      x:Class="Test.MainPage"      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      xmlns:local="using:Test"      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"      mc:Ignorable="d">        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">          <Button Content="Button" HorizontalAlignment="Left" Margin="496,61,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>          <ListView x:Name="lv" HorizontalAlignment="Left" Height="558" Margin="973,43,0,0" VerticalAlignment="Top" Width="256"/>        </Grid>  </Page>  

 

 

这个例子非常的简单,在page上触发点击操作之后将会触发predict中的条件判断

循环999999次 计算量较多,如果在ui线程中直接运行该判断将会造成ui卡死,

但是通过predict 的线程中处理之后 可以快速响应 结果,并将打印输出到listview中

 

当然 我们可以添加多个 predict 条件 以及相应的Action结果  来响应不同的触控,来实现各种自定义手势。

每一种手势将使用一个单独的predict  和相应的Action ,这么做的好处是 将 各种判断抽象化 为工厂模式,方便后期维护,以及2次开发。

我们可以通过程序初始化的时候 配合数据库 在Dictionary里 增加不同的predict 和不同的Action 来完成 配置化。

大家可以通过该模式 扩展,用于多点识别manipulation 或者其他方向。

以上 介绍到此

转载于:https://www.cnblogs.com/CharlesGrant/p/3639386.html

相关资源:WinRT 环境下缺失 Windows API 模拟实现

最新回复(0)