打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
失败也是种经验(Expression Blend 的局限——不支持动态编译)
--------------------------------MyGrid.cs----------------------------------
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace CursorLib
{
    public partial class MyGrid : Grid
    {
        public MyGrid()
        {
            this.Loaded += OnLoaded;
            if (this.InitCodes())
                this.RunDynamicCodes();
        }
        // Begin Set MyButton.Cursor according to the value of ButtonCursor
        public static DependencyProperty ButtonCursorProperty = null;
        // Begin get/set real cursors
        public static void OnButtonCursorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MyGrid fe = (MyGrid)d;
            fe.SetElementCursor(typeof(Button), MyCursors.GetCursor(cursorTypeName));
        }
        // Used in all kind of FrameworkElements
        private void SetElementCursor(Type elementType, Cursor cursorValue)
        {
            for (int i = 0; i < this.Children.Count; i++)
            {
                if (this.Children[i].GetType() == elementType)
                    ((FrameworkElement)this.Children[i]).Cursor = cursorValue;
            }
        }
        // End get/set real cursors
        private void OnLoaded(object obj, RoutedEventArgs args)
        {
             MethodInfo method = dynamicCodesObj.GetType().GetMethod("ResetProperty");
             method.Invoke(dynamicCodesObj,null);
             MessageBox.Show("ButtonCursorProperty:" + GetValue(ButtonCursorProperty).ToString());
        }
        // Begin Dynamic Codes
        private bool InitCodes()
        {
            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DynamicCodeFile.txt");
            if (sr != null)
            {
                this.dynamicCodes = sr.ReadToEnd();
                return true;
            }
            else
            {
                MessageBox.Show("Can not input DynamicCodeFile.txt\n");
                return false;
            }
        }
        private void RunDynamicCodes()
        {
            CompilerResults cr = this.CompileCodes();
            Assembly assembly = cr.CompiledAssembly;
            if (dynamicCodesObj==null)
                dynamicCodesObj = assembly.CreateInstance("CursorLib.DynamicCodes");
            MethodInfo method = null;
            method = dynamicCodesObj.GetType().GetMethod("GetProperty");
            if (ButtonCursorProperty==null)
               // Codes can only run till here in Blend***********************Project failed here***************************
                ButtonCursorProperty = (DependencyProperty)method.Invoke(dynamicCodesObj, null);
            method = dynamicCodesObj.GetType().GetMethod("GetCursorType");
            object[] args=new object[1];
            args[0]="Hand";
            object objCursorType = method.Invoke(dynamicCodesObj,args);
            if (objCursorType!=null)
                SetValue(ButtonCursorProperty, objCursorType);
            method = dynamicCodesObj.GetType().GetMethod("GetCursorTypeName");
            cursorTypeName = (String)method.Invoke(dynamicCodesObj, null);
            MessageBox.Show("cursorTypeName:" + cursorTypeName);
        }
        private CompilerResults CompileCodes()
        {
            // 1.CSharpCodePrivoder
            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
            // 2.ICodeComplier
            ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
            // 3.CompilerParameters
            CompilerParameters objCompilerParameters = new CompilerParameters();
            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            objCompilerParameters.ReferencedAssemblies.Add("PresentationCore.dll");
            objCompilerParameters.ReferencedAssemblies.Add("PresentationFramework.dll");
            objCompilerParameters.ReferencedAssemblies.Add("WindowsBase.dll");
            objCompilerParameters.ReferencedAssemblies.Add("CursorLib.dll");
            objCompilerParameters.GenerateExecutable = false;
            objCompilerParameters.GenerateInMemory = true;
            // 4.CompilerResults
            CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, this.dynamicCodes);
            if (cr.Errors.HasErrors)
            {
                MessageBox.Show("Error in Compiling:");
                foreach (CompilerError err in cr.Errors)
                {
                    MessageBox.Show(err.ErrorText);
                }
                throw new Exception("Error in Compiling!!!");
                return null;
            }
            else
            {
                return cr;
            }
        }
        // End Dynamic Codes
        private String dynamicCodes = null;
        private static String cursorTypeName = null;
        private static object dynamicCodesObj=null;
    }
}
 
 
 
 
-------------------------------MyCursors.cs------------------------------------
using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Collections.Generic;

namespace CursorLib
{
    // Cursors offering to the designers
    public class MyCursor
    {
        private String cursorName = null;
        private Cursor cursorValue = null;
        public MyCursor(String cursorName, Cursor cursorValue)
        {
            this.cursorName = cursorName;
            this.cursorValue = cursorValue;
        }
        public String CursorName
        {
            get { return this.cursorName; }
        }
        public Cursor CursorValue
        {
            get { return this.cursorValue; }
        }
        public bool Exist(String cursorName)
        {
            return cursorName.Equals(this.CursorName);
        }
    }
    public class MyCursors
    {
        private static int EnumLenth = 20;
        private static readonly MyCursor[] myCursors = new MyCursor[EnumLenth];
        public static int IndexOfMyCursor(String cursorName)
        {
            for (int i = 0; i < EnumLenth; i++)
            {
                if (myCursors[i] != null)
                    if (myCursors[i].Exist(cursorName))
                        return i;
            }
            return -1;
        }
        public static bool AddMyCursor(String cursorName, Cursor cursorValue)
        {
            if (IndexOfMyCursor(cursorName) == -1)
            {
                int i = 0;
                while (myCursors[i] != null && i < EnumLenth)
                    i++;
                if (i >= EnumLenth)
                    return false;
                myCursors[i] = new MyCursor(cursorName, cursorValue);
                return true;
            }
            else
                return false;
        }
        public static String GetCursorName(int index)
        {
            if (index < EnumLenth)
                return myCursors[index].CursorName;
            else return null;
        }
        public static Cursor GetCursor(int index)
        {
            if (index < EnumLenth)
                return myCursors[index].CursorValue;
            else return null;
        }
        public static Cursor GetCursor(String cursorName)
        {
            int index = -1;
            if ((index = IndexOfMyCursor(cursorName)) != -1)
                return myCursors[index].CursorValue;
            else return null;
        }
    }
}
 
 
 
 
----------------------------------DynamicCodeFile.txt--------------------------------------
using System;
using System.Windows;
namespace CursorLib
{
 public enum MyCursorType:int
 {
  Default, 
  Hand,
  Max
 }
 public class DynamicCodes : DependencyObject
 {
  public String GetCursorTypeName()
  {
   return GetValue(this.ButtonCursorProperty).ToString();
  }
  public DependencyProperty GetProperty()
  {
   return ButtonCursorProperty=DependencyProperty.Register("ButtonCursor", typeof(MyCursorType) , typeof(MyGrid),
            new FrameworkPropertyMetadata(MyCursorType.Default, new PropertyChangedCallback(MyGrid.OnButtonCursorChanged)));
  }
  public void ResetProperty()
  {
   MyCursorType temp=(MyCursorType)GetValue(this.ButtonCursorProperty);
   SetValue(this.ButtonCursorProperty,MyCursorType.Default);
   SetValue(this.ButtonCursorProperty,temp);
  }
  public object GetCursorType(String name)
  {
   object obj=null;
   for(int i=0;i<(int)MyCursorType.Max;i++)
   {
    if(name.Equals(Enum.GetName(typeof(MyCursorType), i)))
    {
     obj = Enum.ToObject(typeof(MyCursorType),i);
     break;
    }
   }
   return obj;
  }
  private DependencyProperty ButtonCursorProperty=null;
 }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【新提醒】调用系统图片浏览器,返回图片路径、uri
标函数(Cursor)
How To Return an Oracle Ref Cursor to a .NET DataReader Object by Using the .NET Managed Provider fo
python MySQLdb在windows环境下的快速安装、问题解决方式
getContentResolver().query怎么返回有限结果阿
android在异步任务中如何关闭Cursor
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服