|
|
2006年10月3日
1. Declare array object in xaml:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Grid> <ListBox Width="200" Height="200" BorderBrush="Navy" BorderThickness="1"> <ListBox.ItemsSource> <x:Array Type="{x:Type sys:String}"> <sys:String>Paviel Dedved</sys:String> <sys:String>Andriy Shevchenko</sys:String> <sys:String>Paolo Maldini</sys:String> <sys:String>Robert Baggio</sys:String> <sys:String>Alessandro Del Piero</sys:String> </x:Array> </ListBox.ItemsSource> </ListBox> </Grid> </Page> 2. Declare inline code in Xaml:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="MyPage"> <x:Code> <![CDATA[ private void ButtonClickEventHandler(Object sender, RoutedEventArgs e) { MessageBox.Show("Hello Xaml!"); } ]]> </x:Code> <Grid> <Button Width="200" Height="60" Content="Click Me" Click="ButtonClickEventHandler"/> </Grid> </Page> Note that, when specifying inline code in xaml, you also need to specify the x:Class and the Xaml should be compiled before getting execution, which means that XamlReader.Load() method cannot intepret the xaml which contains inline code.
2006年9月26日
Just paste the following xaml into the XamlPad shipped with Microsoft Windows SDK or the XamlPad I create myself, you will get some unexpected surprise, really cool indeed: <Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Margin="10" OpacityMask="#99000000"> <Button/> </Viewbox> This is what I get in my own XamlPad: 
2006年9月23日
摘要: Some one pointed out on MSDN WPF forum post that FindName breaks on DataTemplate, when we want to reference the visual elements inside the data template, we cannot use FindName, then what we can use? ... 阅读全文
2006年8月28日
When we want to declaratively use our custom controls or reference the types we defined in XAML, we should declare the XML namespace to map to the CLR namespace in which the controls and types are defined as the following XAML snippt demonstrates:
<Page x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cc="clr-namespace:Sheva.Windows.Controls;assembly=AvalonLib" /> Recently, I am heavily engaged with writing a resuable WPF control library, and quite often I need to create some pages to test those controls I create, so ever now and then I have to use the cumbersome syntax like above to declare the XML namespace, personally I don't like the syntax, and I thought Microsoft WPF team should come up with a more elegant way of doing it, and in actuality, they did introduce an alternative to do it since the CTP releases. there is a custom attribute called XmlnsDefinitionAttribute in the System.Windows.Markup namespace which allows you to apply to the assembly which contains the types and controls the XAML code would reference.the built-in WPF assemblies such as WindowsBase.dll, PresentationCore.dll, and PresentationFramework.dll all have this attribute applied, and all have http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace mapped with the CLR namespaces introduced by them. and of cause you can use this attribute in your own project, just open the AssemblyInfo.cs file, and append the following directive:
[assembly: XmlnsDefinition("http://schemas.sheva.com/wpf/", "Sheva.Windows.Controls")] Then you can declare the namespace alias in XAML this way:
<Page x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cc="http://schemas.sheva.com/wpf/" /> Wow, this is a bit like the built-in xmlns declaration indeed, really really cool indeed:)
2006年8月24日
Recently, I am tied up writing a custom WPF control called FolderPicker, and I come to the scenario that when I update the data source, I want to let the UI thread to update the control UI, basically, at this time, I should force the dispatcher to process the layout message, in Windows Forms application, If we want to process the windows message such as WM_SIZE, WM_PAINT messages in an event handler, we can call the Application.DoEvents(), then how about in WPF? Actually, WPF doesn't provide such an API, the reason is a bit complicated, and really worth a new post contributing to it, then how to implement such kinda API in WPF, after some examination on this problem, and thanks to Kevin Moore's WPF samples code and Ian Griffiths helpful tips, I finally come up with my own implementation of Application.DoEvents logic.
using System;
using System.Windows;
using System.Windows.Threading;
namespace Sheva.Windows
{
/// <summary>
/// Designates a Windows Presentation Foundation application model with added functionalities.
/// </summary>
public class WpfApplication : Application
{
private static DispatcherOperationCallback exitFrameCallback = new
DispatcherOperationCallback(ExitFrame);
/// <summary>
/// Processes all UI messages currently in the message queue.
/// </summary>
public static void DoEvents()
{
// Create new nested message pump.
DispatcherFrame nestedFrame = new DispatcherFrame();
// Dispatch a callback to the current message queue, when getting called,
// this callback will end the nested message loop.
// note that the priority of this callback should be lower than the that of UI event messages.
DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Background, exitFrameCallback, nestedFrame);
// pump the nested message loop, the nested message loop will
// immediately process the messages left inside the message queue.
Dispatcher.PushFrame(nestedFrame);
// If the "exitFrame" callback doesn't get finished, Abort it.
if (exitOperation.Status != DispatcherOperationStatus.Completed)
{
exitOperation.Abort();
}
}
private static Object ExitFrame(Object state)
{
DispatcherFrame frame = state as DispatcherFrame;
// Exit the nested message loop.
frame.Continue = false;
return null;
}
}
}
The theory behind the code shown above is that when pushing a nested message
loop, the nested message loop will immediately process the messages in the
queue, so the layout or other window messages can get processed. But you should
avoid using nested message loop if at all possible, because as Ian Griffths
says:"bad things happen when you nest message pumps."
what does this mean? let me save it for another day:) Edit: Jessica Fosler has wrriten an awesome article on Application.DoEvents which is really worth a read.
2006年8月17日
现电子工业出版社急招四名翻译人员来翻译Charles Petzold刚刚出炉的新书《Applications = Code + Markup》,要求译者熟悉Windows桌面程序的开发,熟悉.NET编程,对WPF有一定的了解,并具有很强的英语翻译能力。稿费,翻译内容分工等具体事宜容日后再议,请有意者速与我或是梁晶联系。 联系人: 梁晶(Dinla) (电子工业出版社编辑) email: fordin at gmail dot com msn: ccnulj at msn dot com
周勇(Sheva) email: footballism at gmail dot com msn : footballism at hotmail dot com
PS:往dudu让我把这个帖子搁在精华区几天:),先谢过了!!!
2006年5月6日
Eric Gunnerson just brings up a series of posts on some C# trivia that probably most developers don't know about, I just quote some of them which are more relevant to the C# technicality:Language Details
1) How is decimal different from other C# types? 2) What kind of constructor is not legal on structs? Why? 3) What’s the difference between “out” and “[out]”? 4) When you write ulong, what does the runtime see? 5) When can a readonly field be assigned to? 6) How many loop constructs does C# have, and what are they? 7) What is the difference between “ref” and “out”?
C# and the Runtime
1) What interfaces does foreach use in C# 1.1? What about 2.0? 2) What is the name of the attribute that controls the usage of an attribute? 3) What does [FieldOffset(12)] do? 4) What does [IndexerName(“Fred”)] do? Why would you use it?
Do you know the answers?
2006年4月29日
Probably I've been in the WPF world for so long that I need to try something different, yesterday, I come across a Channel9 forum thread on how to use windows forms' Control.CreateParams property, which give me some light on how to customize the style of classical Win32 window. First thing I plan to do is make the System.Windows.Forms.Form window support dropshadow, then I write a custom NumberBox control which only accepts numbers, traditionaly if you want a TexBox to only accept numbers, you can hookup it's KeyDown event to verify the actual keyboard input, in this post, I will show another way to do it, aka using edit control style as the following code demonstrates:using System; using System.Windows.Forms; using System.Runtime.InteropServices;
namespace CreateParamsDemo { public partial class MyCustomForm : Form { private static Int32 CS_DROPSHADOW = 0x00020000; public MyCustomForm() { InitializeComponent(); NumberBox box = new NumberBox(); box.Multiline = true; box.Dock = DockStyle.Fill; this.Controls.Add(box); }
protected override CreateParams CreateParams { get { CreateParams parameters = base.CreateParams; parameters.ClassStyle |= CS_DROPSHADOW;
return parameters; } } }
public class NumberBox : TextBox { private static Int32 ES_NUMBER = 0x2000; protected override CreateParams CreateParams { get { CreateParams parameters = base.CreateParams; parameters.Style |= ES_NUMBER;
return parameters; } } } } Now, the code is up and ready for running, here comes the screenshot:

Please pay closer attention to the shadow around the window border, you will get the dropshadow for the window, and if you press any key except number keys, a balloon tool tip will show up, saying "Unacceptable Character", the implementation is quite simple, but you can actually get your job done quickly, you know, sometimes, simple approach is the best:)
2006年4月27日
Several days ago, Dflying Chen just shown us how to implement in-place editing functionality using ASP.NET Atlas framework, and the custom control he writes called InPlaceEditingInput is pretty cool indeed. In this post, I will show you how to achieve the same thing in WPF, and also demonstrate how the WPF makes our lives as programming hobbyists much easier:) WPF introduces a new concept called Control template, which means that you can replace the visual tree of any WPF element by defining a new ControlTemplate for the targeting element, and this capability makes writing a custom In-Place Editing WPF control much easier. the following code lists the ControlTemplate I define for the custom textbox control called InfoTextBox:<Style TargetType="{x:Type itb:InfoTextBox}"> <Setter Property="FontSize" Value="20"/> <Setter Property="Margin" Value="5"/> <Setter Property="BorderBrush" Value="Black"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type itb:InfoTextBox}"> <Grid Margin="0"> <Border Name="background" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Decorator x:Name="PART_ContentHost" VerticalAlignment="Top" Margin="0"/> </Border> <Label Content="{TemplateBinding TextBoxInfo}" x:Name="Message" Foreground="Gray" Opacity="0.9" FontSize="{TemplateBinding FontSize}" FontStyle="Italic" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0"/> </Grid> <ControlTemplate.Triggers>
<MultiTrigger> <MultiTrigger.Conditions> <Condition Property="HasText" Value="False"/> <Condition Property="IsFocused" Value="True"/> </MultiTrigger.Conditions> <MultiTrigger.EnterActions> <BeginStoryboard Storyboard="{StaticResource enterGotFocus}"/> </MultiTrigger.EnterActions> <MultiTrigger.ExitActions> <BeginStoryboard Storyboard="{StaticResource exitGotFocus}"/> </MultiTrigger.ExitActions> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="HasText" Value="False"/> <Condition Property="IsFocused" Value="False"/> </MultiTrigger.Conditions> <Setter TargetName="background" Property="Visibility" Value="Hidden"/> </MultiTrigger>
<Trigger Property="HasText" Value="True"> <Setter TargetName="Message" Property="Visibility" Value="Hidden"/> </Trigger>
<Trigger Property="IsEnabled" Value="false"> <Setter TargetName="background" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> <Trigger Property="Width" Value="Auto"> <Setter Property="MinWidth" Value="100"/> </Trigger>
<Trigger Property="Height" Value="Auto"> <Setter Property="MinHeight" Value="20"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> The above XAML markup is really worth a detailed explanation, WPF actually supports styling, so what's styling? well, you can think of WPF styling as the CSS for XHTML elments, with styling, you can easily change the look and feel of the WPF controls by grouping the style properties of a speficied control in a common location(usually in the ResourceDictionary collection). Another thing you might note is that I define a lot of triggers inside the style element, so what's triggers? a trigger represents a specific condition for a property value, when a trigger condition is met, it wil cause the associated setter XAML statement to change a control property's value, in the above XAML markup for instance, when the "HasText" property is set to true, it will change the "Message" Label element's Visibility property to "Hidden". As I mentioned earlier in this post, InfoTextBox is a custom control which is derived from System.Windows.Controls.TextBox, the actual implementation of this custom control is nothing more than adding two additional dependency properties for the base TextBox control, one is TextBoxInfoProperty, and the other is HasTextProperty as the following code demonstrates:
 InfoTextBox Implementation public class InfoTextBox : TextBox { public InfoTextBox(): base(){}
static InfoTextBox() { TextProperty.OverrideMetadata(typeof(InfoTextBox), new FrameworkPropertyMetadata(new PropertyChangedCallback(TextPropertyChanged))); }
public static readonly DependencyProperty TextBoxInfoProperty = DependencyProperty.Register( "TextBoxInfo", typeof(String), typeof(InfoTextBox), new PropertyMetadata(String.Empty));
public string TextBoxInfo { get { return (String)GetValue(TextBoxInfoProperty); } set { SetValue(TextBoxInfoProperty, value); } }
private static readonly DependencyPropertyKey HasTextPropertyKey = DependencyProperty.RegisterReadOnly( "HasText", typeof(Boolean), typeof(InfoTextBox), new FrameworkPropertyMetadata(false));
public static readonly DependencyProperty HasTextProperty = HasTextPropertyKey.DependencyProperty;
public Boolean HasText { get { return (Boolean)GetValue(HasTextProperty); } }
static void TextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { InfoTextBox itb = (InfoTextBox)sender;
Boolean actualHasText = itb.Text.Length > 0; if (actualHasText != itb.HasText) { itb.SetValue(HasTextPropertyKey, actualHasText); } } } After all those plumbing work completes, we can use the tailer-made control anywhere as we would like:
<GroupBox Header="Comment Form" FontSize="20" Margin="10" BorderBrush="LightBlue" BorderThickness="1" Padding="10"> <Grid Margin="0"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions>
<Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <itb:InfoTextBox TextBoxInfo="Put Your Name Here" TextWrapping="NoWrap" Grid.Row="0" Grid.Column="0"/> <itb:InfoTextBox TextBoxInfo="Put Your Email Address Here" TextWrapping="NoWrap" Grid.Row="1" Grid.Column="0"/> <itb:InfoTextBox TextBoxInfo="Put Your Comment Here" TextWrapping="NoWrap" Grid.Row="2" Grid.Column="0"/> <Button Content="Submit" Grid.Row="3" Grid.Column="0" Width="100" Height="30" HorizontalAlignment="Left"/> </Grid> </GroupBox> Here is the screenshot of my functionally complete in-place editing UI:
 For complete source code, please check here.
2006年4月23日
摘要: ILinkedList interface definitionCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->usingSystem;usingSystem.Collections.Generic;namespaceSheva.Collections... 阅读全文
|