Saturday, July 22, 2017

Using .NET Standard 2.0.0 preview in Visual Studio

.NET Standard is the new common library for all .NET platforms (.NET Framework, .NET Core, Xamarin). There is a good introduction on the .NET Blog (https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/).
You can create a new .NET Standard project within Visual Studio 2017. Click on "Create new project...". Then you can select the .NET Standard entry to create a Class Library (.NET Standard). As .NET Framework you can choose a version equal or greater than 4.6.1 to support .NET Standard 2.0.



After creating the project, you can see under Dependencies the NETStandard.Library.



You can choose "Manage NuGet Packages..." from the context menu to have a closer look. You can see that version 1.6.1 is used, since it is the latest stable version that is compatible with the .NET Framework 4.7.



Furthermore you can see the messages "Blocked by project" and "Following versions are unavailable due to additional constraints in the project or packages.config" for version 2.0.0-preview2-25401-01 and some others.

Following entries can you see in the project file.
<Project Sdk="Microsoft.NET.Sdk"> 
  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup> 
</Project>

Nevertheless you can install the 2.0 version via Package Manager Console. Just enter

Install-Package NETStandard.Library -IncludePrerelease -Version 2.0.0-preview2-25401-01

If you look again in the project file, you will see 3 lines that were added.
<Project Sdk="Microsoft.NET.Sdk"> 
  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup> 
  <ItemGroup>
    <PackageReference Update="NETStandard.Library" Version="2.0.0-preview2-25401-01" />
  </ItemGroup> 
</Project>

But this will not solve the problem. .NETStandard 2.0 is not used properly. In Visual Studio 15.2 you can not choose .NERStandard 2.0.



It is supported with Visual Studio 15.3.






<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

Saturday, January 9, 2016

The differences between TextBlock and Label in WPF

You can use two elements to show text in GUI, so-called "Label". These are
  • Label: <Label>Text</Label>
  • TextBlock: <TextBlock>Text</TextBlock>
Both produce nearly the same result. But there are differences. TextBlock inherits from FrameworkElement whereas Label inherits from ContentControl --> Control --> FrameworkElement.

This difference lead to all differences that these elements have.

One consequence can directly shown if we rewrite the code from above.
  • Label: <Label Content="Text" />
  • TextBlock: <TextBlock Text="Text" />
TextBlock has no Content, just Text. So TextBlock is restricted to strings, while Label can also have other types as content like images.
<Label>
    <Image Source="text.png" />
</Label>
One other frequently mentioned consequence from the different roots of Label and TextBlock is that Label supports Access Keys (Mnemonics), while TextBlock don't.
<Label Content="_Text"
       Target="{Binding ElementName=InputField}" />
<TextBox x:Name="InputField" />

Furthermore a Label is grayed out, if its IsEnabled property is false, whereas TextBlock isn't. You can also use the Template property for a custom control template or the ContentTemplate property to apply a DataTemplate to its content.

Summarized you can say the difference is that with a Label you can do more than with a TextBlock. You can do whatever a ContentControl can do, while a TextBlock can only do what a FrameworkElement can do. But the huge advantage of the TextBlock is that it is much more lightweight.

So use TextBlock, whenever you just need text. If text is insufficient, use Label.