AllowsTransparency and WindowsFormsHost
We just spend a bunch of time trying to figure out why our WinForms control was not showing up on a WPF window after we started using EssentialWindow from Codeplex. After trying few things and searching we found that if you have AllowsTransparency set to true on the root window element, then WindowsFormsHost will not be displayed.
From GuiGuys: “Warning: If you have the attribute AllowsTransparency=True on the root Window element, the WindowsFormsHost will not be displayed. You will not get any compiler errors, but the content will simply not be displayed. The way WPF handles transparency is not supported by Windows Forms, so if the window allows transparency, the Winforms content cannot be properly rendered… I learned this the hard way. It took a while to figure out why my controls weren’t displaying on my GUI!”
xsd.exe Location
I had to search C:\Program Files last time I wanted to use this utility, adding it to the environment variables now.
It resides in
C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin
Very handy if you want to generate an xml schema from xml files or a dll and similar other conversions.
http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx
Trace Logging with Post Sharp
Initially I was thinking of writing a how-to myself, but then found a few good posts on getting started with PostSharp for trace logging:
- PostSharp AOP for .NET: Shows how post-compiler weaves the AO code into the IL
- Logging with PostSharp and NLog: Shows how we can add the PostSharp logging attribute to an assembly, this way the logging framework is decoupled from the rest of the code.
NotifyPropertyChanged with PostSharp
If you are using WPF in your applications then it is highly likely that you’ve had to implement INotifyPropertyChanged on your POCOs for binding. A neat way of implementing this functionality without CPP is by using PostSharp Attributes.
“PostSharp is the leading aspect framework (AOP) for Microsoft .NET. It will let you make custom attributes that add behaviours to your code.”
PostSharp Binaries come with a Binding sample which includes NotifyPropertyChangedAttribute. Now all you need to do it decorate your POCOs with the [NotifyPropertyChanged] attribute and you will get property change notifications. Elegant and easy!
Thanks to Paul Stovell for introducing us to PostSharp.
Implementing an Interface in IronPython
In IronPython interface implementation declaration is same as inheritance declaration:
Here Banana is the parent class
class Mango(Banana):
…
Here IFruit is the interface implemented by the class
class Banana(IFruit):
…
Stumbled upon this PEP while figuring out how to do this and realised that CPython doesn’t support interface implementation.
IronPython Script to DLL
IronPython script can be compiled into a DLL using the Python command line compiler (pyc.py) that can be downloaded along with other samples from here:
http://ironpython.codeplex.com/Wiki/View.aspx?title=Samples
OR by directly using the clr.CompileModules function:
import clr
clr.CompileModules(“Sample.dll”, “Sample.py”)
Initially I thought that consuming classes declared in these script dlls from C# would be similar to referencing any other .NET dll. But after reflecting through the generated dll it became clear that it does not publish statically generated classes. Reading more on this topic, it became clear why this is done:
“ While the assemblies produced by the IronPython Hosting APIs are true .NET assemblies, the dynamic nature of the Python language makes it difficult to use these from other .NET languages. In short, this means that attempting to import Python types into other .NET languages such as C# is not recommended.” Source
So compiling your script into a dll is a good option if you are looking for protecting/Obfuscating your code before distribution, but for consumption of these dlls you have to stick with DLR.
Integrating IronPython 2.6
I recently downloaded the latest version of IronPython (v2.6 Alpha 1) with an aim of using it as a data investigation engine for our applications.
If you are using older version of IronPython then here’s are great article you can refer to: http://www.codeproject.com/KB/dotnet/ironpython.aspx
With the new version, setting up the engine for script execution is straight forward:
using IronPython.Hosting;
using IronPython.Compiler;
using Microsoft.Scripting.Hosting;public void ExecuteScript(string fileName)
{
var engine = Python.CreateEngine();
var scope = engine.CreateScope();
engine.ExecuteFile(fileName, scope);
}
If your application uses dependency injection mechanism for composition, then using it from the scripts is as simple as passing in your container:
engine.SetVariable(scope, “dependenciesContainer”, dependenciesContainer);
That’s it! Now you can start writing scripts for extension. Registering appropriate data investigation/manipulation services with the container would allow you to control the extent of extension
Implementing M-V-VM pattern in WPF
- Model-View-ViewModel is a design pattern used to create client applications that take advantage of data-binding in WPF
- View = Thing you see on the screen that the user interacts with
- View-Model = something that the view binds to/ the visual state of the data
- Model = your data object
- Interaction between the view and the view-model is done by data binding
- Things to know before approaching this pattern:
- Become familiar with XAML including data-binding and use of commands
- Workflow
- Consider how the view is going to bind to the View-Model
- What properties are expected?
- Create the view and identify the properties required for binding
- Create the view-model that exposes the properties required by the view
- Consider how the view is going to bind to the View-Model
- Building a View-Model
- Consider building presentation friendly adapter around the model object
- Think about taking a view and creating a VM that stores the state of the view
- When you build a VM, pass in the model (data model) to the constructor so that you can easily connect and use the model from within the VM
- VM is just an adapter or a wrapper class where it is wrapping the original model, wrapping can be done in the constructor or using the lazy load method i.e. on property getters
- VMs can also be used to wrap other VMs
- Set the VM as the data context of the view
- Add state properties to the VM as required
- Consider defining interface for the VM so that the contract between the View and the VM is well defined
- Instead of using click handler use command binding. This will make unit testing easier as commands are exposed by the VM. Command in WPF implement ICommand interface
- Files you might end up with:
- SomeView.xaml
- ISomeViewModel.cs
- SomeViewModel.cs
- ISomeModel.cs
- SomeModel.cs
Margins in WPF and CSS
In WPF margin = Left Top Right Bottom
In CSS margin = Top Right Bottom Left
Leave a Comment
Leave a Comment
Leave a Comment



