USB HID Code project
Making USB C# friendly
Detect USB Device using Vendor and Product ID
Tuesday, 15 December 2009
Monday, 7 December 2009
WPF : UserControl and Focus managment
Letting a parent Usercontrol know about its child focus
Setting the Focused Element in Xaml:
<stackpanel focusmanager.focusedElement="{Binding ElementName=txtValue}"></stackpanel>
Setting the Focused Element in Xaml:
<stackpanel focusmanager.focusedElement="{Binding ElementName=txtValue}"></stackpanel>
Monday, 23 November 2009
Friday, 20 November 2009
Wednesday, 18 November 2009
ASP.net Problems
- Starting IIS on Vista for the first time Skype was found to be using port 8080 so stopped Skype and restarted iis without a problem.
- Error message CS0016: Could not write to output file 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\etc etc .dll' -- 'Access is denied. Add full permissions for 'Network Service' & 'YourComputerName\IIS_IUSERS to:
- C:\Windows\Temp
- C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
- ASP.net local iis problem when trying to run locally
Error message not very helpfull, eventually fixed by removing directives in the Web.Config for
ScriptModule
WebExtensions
Not 100% sure why though they are probably in the Machine.config file and therefore created a duplicate which was only detected when running outside debug environment
Tuesday, 17 November 2009
Friday, 13 November 2009
C# Constructors
For some reason I always forget how to call one constructor from another. Of course now I've written it down I'll probably never need to refer to it again but anyway:
public Car(string model, int noWheels){}
Car(string model) : this(model,4){}
public Car(string model, int noWheels){}
Car(string model) : this(model,4){}
Thursday, 5 November 2009
WPF: NUnit Testing when using the WPF Dispatcher
I have encoutered problems when using NUnit to test a ViewModel which has ThreadSafeObservableCollection. The thread safety comes about through using the Dispatcher and when you call Dispatcher.BeginInvoke, you are instructing the dispatcher to run the delegates on its thread when the thread is idle.
When running unit tests, the main thread will never be idle. It will run all of the tests then terminate.
To overcome this I have written a trap which looks for the NUnit Thread Name. A true result and the ThreadSafeCollection proceeds without using the Dispatcher.
private bool CheckDispatcherAccess()
{
if (_dispatcher.Thread.Name == "TestRunnerThread")
{
return true;
}
return _dispatcher.CheckAccess();
}
...
Other references to the Dispatcher issue when using NUnit
Using the WPF Dispatcher in NUnit Tests
When running unit tests, the main thread will never be idle. It will run all of the tests then terminate.
To overcome this I have written a trap which looks for the NUnit Thread Name. A true result and the ThreadSafeCollection proceeds without using the Dispatcher.
private bool CheckDispatcherAccess()
{
if (_dispatcher.Thread.Name == "TestRunnerThread")
{
return true;
}
return _dispatcher.CheckAccess();
}
...
Other references to the Dispatcher issue when using NUnit
Using the WPF Dispatcher in NUnit Tests
Monday, 2 November 2009
LINQ: Update Data
var product = (from p in dataContext.Products
where p.ProductID == 1
select p).Single();
product.Name = "New Product Name";
dataContext.SubmitChanges();
Using LINQ with MVC
The following code using the Attach method on the Linq entity will throw an exception as per Stephen Walther's excellent article Stephen Walther blog archive. This is a concurrency issue and can be remedied in 2 ways as per Stephen's article.
public ActionResult Edit(DxDataBook editedPart)
{
try
{
var originalPart = (from p in dc.DxDataBooks
where p.P_D_Num == editedPart.P_D_Num
select p).First();
if (!ModelState.IsValid)
{
return View(originalPart);
}
dc.DxDataBooks.Attach(editedPart,true);
dc.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
where p.ProductID == 1
select p).Single();
product.Name = "New Product Name";
dataContext.SubmitChanges();
Using LINQ with MVC
The following code using the Attach method on the Linq entity will throw an exception as per Stephen Walther's excellent article Stephen Walther blog archive. This is a concurrency issue and can be remedied in 2 ways as per Stephen's article.
public ActionResult Edit(DxDataBook editedPart)
{
try
{
var originalPart = (from p in dc.DxDataBooks
where p.P_D_Num == editedPart.P_D_Num
select p).First();
if (!ModelState.IsValid)
{
return View(originalPart);
}
dc.DxDataBooks.Attach(editedPart,true);
dc.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Friday, 30 October 2009
WPF : XPS Printing Support
Printing ListView using XPS
References required
using System.Windows.Xps.Packaging;
using System.Windows.Xps;
held in Assys
Sytem.Printing.dll
ReachFramework.dll
References required
using System.Windows.Xps.Packaging;
using System.Windows.Xps;
held in Assys
Sytem.Printing.dll
ReachFramework.dll
Thursday, 15 October 2009
WPF : Non scrolling ListView inside a StackPanel!!!!
Compositional UI designers beware! If you use the StackPanel and attempt to load a view containing any kind of ListView inside that StackPanel's tree, said ListView will never scroll vertically.
Same problem when used in a grid with Height set to Auto.
Same problem when used in a grid with Height set to Auto.
Tuesday, 22 September 2009
WPF: Defining a style based on the base style
<Style TargetType="ToggleButton" BasedOn="{StaticResource {x:Type ToggleButton}}">
The VS2008 IDE fails to render the XAML correctly if you try to modify an existing Style for a Component. It does still compile and excute correctly however.
A ListViewItem Style for example:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}">
<Style.Triggers>
<Trigger Property="ListViewItem.IsMouseOver" Value="True">
<Setter Property="ListViewItem.IsSelected" Value="true"></Setter>
</Trigger>
</Style.Triggers>
</Style>
The VS2008 IDE fails to render the XAML correctly if you try to modify an existing Style for a Component. It does still compile and excute correctly however.
A ListViewItem Style for example:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}">
<Style.Triggers>
<Trigger Property="ListViewItem.IsMouseOver" Value="True">
<Setter Property="ListViewItem.IsSelected" Value="true"></Setter>
</Trigger>
</Style.Triggers>
</Style>
Tuesday, 15 September 2009
MVVM: Silverlight versus WPF
In order to use the Command model for Silverlight it is necessary to download the Prism for Silverlight extensions
Silverlight Prism extension
Alternatively it is possible to place a click event inside view behind on Silverlight view then cast Datacontext to ViewModel and execute methods
add to the View code behind
Button_Click(object sender,RoutedEventArgs e)
{
var data = this.DataContext as ViewModel;
data.MyMethod();
}
Silverlight Prism extension
Alternatively it is possible to place a click event inside view behind on Silverlight view then cast Datacontext to ViewModel and execute methods
add to the View code behind
Button_Click(object sender,RoutedEventArgs e)
{
var data = this.DataContext as ViewModel;
data.MyMethod();
}
Saturday, 12 September 2009
Friday, 11 September 2009
WPF
Test for Design Mode:
DesignerProperties.GetIsInDesignMode
Access framework property Metadata:
FrameworkPropertyMetaData fpmd = new TextBox.TextProperty.GetMetadata(typeof(TextBox)) as FrameworkPropertyElement;
bool bindingDirectionByDefault = fpmd.BindsTwoWayByDefault;
Changing List Selected Item on TextBox GotFocus:
<listview.itemcontainerstyle>
<style targettype="ListViewItem">
<setter property="HorizontalContentAlignment" value="Stretch"></setter>
<eventsetter event="GotFocus" handler="TextBox_GotFocus"></eventsetter>
</style>
</listview.itemcontainerstyle>
private void Item_GotFocus(object sender, RoutedEventArgs e)
{
ListViewItem item = sender as ListViewItem;
this.parameterList.SelectedItem = item.DataContext;
}
DesignerProperties.GetIsInDesignMode
Access framework property Metadata:
FrameworkPropertyMetaData fpmd = new TextBox.TextProperty.GetMetadata(typeof(TextBox)) as FrameworkPropertyElement;
bool bindingDirectionByDefault = fpmd.BindsTwoWayByDefault;
Changing List Selected Item on TextBox GotFocus:
<listview.itemcontainerstyle>
<style targettype="ListViewItem">
<setter property="HorizontalContentAlignment" value="Stretch"></setter>
<eventsetter event="GotFocus" handler="TextBox_GotFocus"></eventsetter>
</style>
</listview.itemcontainerstyle>
private void Item_GotFocus(object sender, RoutedEventArgs e)
{
ListViewItem item = sender as ListViewItem;
this.parameterList.SelectedItem = item.DataContext;
}
Friday, 4 September 2009
Threading Issues
WPF Threading issues
Exception Thrown "The calling thread cannot access this object because a different thread owns it".
When hooking up Images using a MVVM pattern and background threads this
Exception Thrown "The calling thread cannot access this object because a different thread owns it".
Use Image.Freeze() command to prevent this.
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5975d3f5-9771-442f-a11e-183b4969e38d/
Again MVVM this time button does not update its valid state following a background thread.
Normally the WPF infrastructure updates every command`s enabled/disabled state but in this case need to refresh the command`s enabled/disabled state manually.
Use InvalidateRequestQuery.
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d2cf8ff9-eecd-40ec-b3a1-c8c8e39378d9
This is a added to the event subscriber in the MV
Application.Current.Dispatcher.BeginInvoke(new Action CommandManager.InvalidateRequerySuggested));
Exception Thrown "The calling thread cannot access this object because a different thread owns it".
When hooking up Images using a MVVM pattern and background threads this
Exception Thrown "The calling thread cannot access this object because a different thread owns it".
Use Image.Freeze() command to prevent this.
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5975d3f5-9771-442f-a11e-183b4969e38d/
Again MVVM this time button does not update its valid state following a background thread.
Normally the WPF infrastructure updates every command`s enabled/disabled state but in this case need to refresh the command`s enabled/disabled state manually.
Use InvalidateRequestQuery.
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d2cf8ff9-eecd-40ec-b3a1-c8c8e39378d9
This is a added to the event subscriber in the MV
Application.Current.Dispatcher.BeginInvoke(new Action CommandManager.InvalidateRequerySuggested));
Subscribe to:
Posts (Atom)