最近在MXS里用WPF写了UI,图片资源都是在Xaml里写绝对路径,这样子不方便而且容易报错
下面这个方法可以在MXS里正确加载图片资源
这是UI
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid Background="Black"> <Image x:Name="image2" HorizontalAlignment="Left" Height="100" Margin="189,102,0,0" VerticalAlignment="Top" Width="100"/> </Grid> </Window> 在MXS里的代码 tempStr= "<Window xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Title=\"MainWindow\" Height=\"350\" Width=\"525\"> <Grid Background=\"Black\"> <Image x:Name=\"image2\" HorizontalAlignment=\"Left\" Height=\"100\" Margin=\"189,102,0,0\" VerticalAlignment=\"Top\" Width=\"100\"/> </Grid> </Window> " Xaml = dotnetclass "System.Windows.Markup.XamlReader" tempUI = Xaml.Parse tempStr<span style="font-family: Arial, Helvetica, sans-serif;"> </span> fn disable_accelerators = ( enableAccelerators = false ) fn enable_accelerators = ( enableAccelerators = true ) /*Setting the Focus od the window to capture all Keyboard events while the window is "Active" or focused dotNet.addEventHandler tempUI "Activated" disable_accelerators*/ /*Make sure we return control when we are done*/ dotNet.addEventHandler tempUI "Deactivated" enable_accelerators /*New to Max 2010, we use this to make sure Maxscript Garbage collection does not destory our Functions and events before we are done with them dotNet.setLifetimeControl tempUI #dotnet*/ fn wpfPictureSource filePath uiControl= ( binReader = (dotNetObject "System.IO.BinaryReader" ((dotNetClass "System.IO.File").Open filePath (dotNetClass "System.IO.FileMode").Open)) fileInfo = dotNetObject "System.IO.FileInfo" filePath byteType = dotNetClass "System.Byte[]" bytes = dotNet.ValueToDotNetObject (binReader.ReadBytes(fileInfo.Length as integer)) byteType binReader.Close() newBitmap = dotNetObject "System.Windows.Media.Imaging.BitmapImage" newBitmap.BeginInit() newBitmap.StreamSource = dotNetObject "System.IO.MemoryStream" bytes newBitmap.EndInit() uiControl.Source = newBitmap ) filePath = @"F:\UE4常用\bu_N.jpg" wpfPictureSource filePath (tempUI.FindName "image2") tempUI.show()上述是对Image这个空间的,然后由于我某些button也需要引用图片资源,但是button.Background的值是ImageBrush(详情见https://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.background(v=vs.95).aspx)
所以我尝试去设置button.Background时发现了error
因此用以下方式去设置
tempImageBrush = dotNetObject "System.Windows.Media.ImageBrush" tempImageBrush.ImageSource = newBitmap setProperty (tempUI.FindName "btn") #Background tempImageBrush参考01:http://blog.sina.com.cn/s/blog_5c9288aa0102vm3i.html
参考02:http://www.cnblogs.com/ErinCodeMM/archive/2011/04/07/2008819.html
