Can't find System.Windows.GridLength in ..Net5.0 - c#

I'm having trouble converting an application from .NetFramework to .Net5.0. For example, I have a variable of type System.Windiws.GridLength correctly set in Properties.Settings.Default that I can no longer refer to in Net5.0 because it is nowhere to be found (I get the error "System.Windows.GridLength type is not defined" by inserting the type name in the appropriate field in the type selection window shown by choosing the 'Browse' item from the pop-up menu in the 'Type' column of the Settings window). What i am missing?
**** EDIT ************
My project's .csproj as requested:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<StartupObject>Stats4Betting_NetCore.App</StartupObject>
<ApplicationIcon>Resources\soccer_boom_alt.ico</ApplicationIcon>
<Platforms>AnyCPU;x64;x86</Platforms>
</PropertyGroup>
<ItemGroup>
....
</ItemGroup>
</Project>

Related

How to create multiple partial classes of same xaml file

I need to separate code from a single code behind to multiple partial classes, but didn't know how I can achieve this, I saw in one msdn code sample that this is possible, could you please tell me how I can achieve this.
I know I can move to MVVM, but for now I just need to organize code through partial classes.
Please have a look at the below screenshot.
You can do that by creating the additional files in the naming convention you like and adding the partial class to them. code will compile and work same as before.
MainPage.xaml.cs:
public partial class MainPage : Page {...}
MainPage.Flash.xaml.cs:
public partial class MainPage {...}
To get what you show in the attached image will involve manually editing the project file. Open you project file in a text editor and look at how it is done for your current code behind and the process is the same
Here is an example of what the image you provided would look like in the project file
<ItemGroup>
<Page Include="MainPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="MainPage.ExposureValue.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="MainPage.Flash.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="MainPage.Focus.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="MainPage.IsoSpeed.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="MainPage.Shutter.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="MainPage.WhiteBalance.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="MainPage.Zoom.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
</ItemGroup>
Note that with new VS (e.g. VS 2022) and the new SDK-style .csproj files, there are already hidden/automatically generated compile elements for .cs files, so you get an error doing the same as #Nkosi suggests above, as you end up with effectively duplicated compile elements.
Little tweak seems to work for me in these newer csproj files, i.e. just a None (instead of compile) element for each file you want to nest.
<ItemGroup>
<None Include="MainWindow.Helpers.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
</None>
</ItemGroup>

C#-cannot view properties of a reference in bin folder

Click here for screenshot.on clicking the reference it is not showing the properties, How to set embed interop type to false?
Go to your .csproj file.. Edit it and Change the value to False
<EmbedInteropTypes>False</EmbedInteropTypes>
Exemple of .csproj file :
<ItemGroup>
....
<Reference Include="Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
....
</ItemGroup>

Copy files based on release or debug build

I am making some changes in my csproj file so when I build debug it will copy a set of debug files and when I build release it will copy a set of release files.
The Start of the csproj:
<PropertyGroup>
...
<FlexNetInput></FlexNetInput>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
...
<FlexNetInput>"..\..\..\..\utilities\FlexNet\Debug\Native\"</FlexNetInput>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
...
<FlexNetInput>"..\..\..\..\utilities\FlexNet\Release\Native\"</FlexNetInput>
</PropertyGroup>
At the end of the csproj this is what I have:
<Target Name="AfterBuild">
<!-- Copy Native DGI DLL's -->
<CreateItem Include="..\..\..\..\utilities\dgi\DgiNative\**\*.*">
<Output TaskParameter="Include" ItemName="NativeDgiFiles" />
</CreateItem>
<Copy SourceFiles="#(NativeDgiFiles)" DestinationFiles="#(NativeDgiFiles->'$(OutputPath)\Native\%(RecursiveDir)%(Filename)%(Extension)')" />
<!-- Copy Native FlexNet DLL's -->
<CreateItem Include="'$(FlexNetInput)'**\*.*">
<Output TaskParameter="Include" ItemName="NativeReleaseFlexNetFiles" />
</CreateItem>
<Copy SourceFiles="#(NativeReleaseFlexNetFiles)" DestinationFiles="#(NativeReleaseFlexNetFiles->'$(OutputPath)\Native\%(RecursiveDir)%(Filename)%(Extension)')" />
The copy of the dgi file works fine, but because the location of the FlexNet file is different depending on if it is release or debug I cant get it to work.
The error I am getting is:
Cannot evaluate the item metadata "%(Filename)". The item metadata "%(Filename)" cannot be applied to the path "'"........\utilities\FlexNet\Debug\Native\"***.*'". Illegal characters in path.
I don't think I am using the properties right, any ideas?
Solved, This is how I got it working:
<CreateItem Include="..\..\..\..\utilities\FlexNet\$(Configuration)\Native\**\*.*">
<Output TaskParameter="Include" ItemName="NativeFlexNetFiles" />
</CreateItem>
<Copy SourceFiles="#(NativeFlexNetFiles)" DestinationFiles="#(NativeFlexNetFiles->'$(OutputPath)\Native\%(RecursiveDir)%(Filename)%(Extension)')" />
These days you should probably never need CreateItem anymore. ItemGroups can be placed inside targets, and will be evaluated when the target runs. Also since your output directory has the same name as your Configuration you can use it as a property. Together this gives
<ItemGroup>
<NativeReleaseFlexNetFiles Include=
"..\..\..\..\utilities\FlexNet\$(Configuration)\Native\**\*.*"/>
</ItemGroup>
in your AfterBuild target.

Building manually with MS Build fails at references [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am learning the WPF structure in C# and XAML. I believe the best way to learn is without the use of any tools other than a compiler and a basic text editor. Unfortunately, because I refuse to use Visual Studio I have had quite the difficulty running my code.
My Question: I have a basic WPF application with the following structure:
+HelloWorld
|-app.xaml
|-app.proj
|-compile.proj
|+main
|-mainWindow.xaml
|+obj
|+Debug
|-app.g.cs
|+main
|-mainwindow.g.cs
The files of interest are mainwindow.xaml, app.xaml, app.proj, and compile.proj.
I first compile the app.xaml and mainwindow.xaml into generated c# code with this:
msbuild compile.proj /tv:4.0
where compile.proj looks like:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="Microsoft.Build.Tasks.Windows.MarkupCompilePass1"
AssemblyFile="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll" />
<Target Name="MarkupCompilePass1Task">
<MarkupCompilePass1
AssemblyName="HelloWorld"
Language="C#"
OutputType="WinExe"
OutputPath="obj\Debug\"
ApplicationMarkup="App.xaml"
PageMarkup="main\mainwindow.xaml"
References="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Xaml.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationCore.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\WindowsBase.dll" />
</Target>
</Project>
after this (which builds successfully) I compile the generated c# code into an executable. with this:
msbuild app.proj /tv:4.0
The code for app.proj is as follows:
<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="Microsoft.Build.Tasks.Windows.MarkupCompilePass1"
AssemblyFile="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll" />
<PropertyGroup>
<appname>HelloWorld</appname>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Core.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Xaml.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\WindowsBase.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationCore.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework.dll" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="app.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="G:\Docs\Programming\work\WPF\HelloWorld2\obj\debug\App.g.cs">
<DependentUpon>app.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="main/mainwindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="G:\Docs\Programming\work\WPF\HelloWorld2\obj\debug\main\mainwindow.g.cs">
<DependentUpon>main\mainwindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<Target Name="Compile">
<CSC
Sources = "#(Compile)"
References="#(Reference)"
OutputAssembly ="$(appname).exe">
<Output
TaskParameter = "OutputAssembly"
ItemName = "EXEFile" />
</CSC>
</Target>
</Project>
This also builds successfully. This then creates helloworld.exe. unfortunately when ran, the executable fails saying the following
Unhandled Exception: System.IO.IOException: Cannot locate resource 'main/mainwindow.xaml'.
After this it lists the function trail which ends in:
at HelloWorld.app.Main()
The only other two written files are app.xaml and mainwindow.xaml
app.xaml:
<Application x:Class="HelloWorld2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="main/mainwindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
mainwindow.xaml:
<Window x:Class="HelloWorld.mainwindow"
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>
<TextBox Height="23" Width="120" Margin="78,89,319,207" Name="MainTextBox" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left"></TextBox>
</Grid>
</Window>
I have no clue why it can't find the proper xaml file. am I using a improper file structure? Am I missing a compile option on either of the msbuild calls? Thank you for your help.
That MarkupCompilePass1 task is used by MsBuild to perform a preliminary build. Why? Because in order for the XamlCompilePass to be successful, it needs to reference local types in a compiled binary.
Rather than reinvent the wheel, poorly, check out the structure of a WPF project file created by Visual Studio. You'll notice such things like default binary references and default property group values like the following:
<ProjectGuid>{CAFEFCAD-429A-4C61-BD3A-157F042E1FE7}</ProjectGuid>
<OutputType>WinExe</OutputType>
This should get you started on your way to building your WPF projects with MsBuild.
Regarding your issues with Visual Studio and MsBuild, did you know that MsBuild now ships with Visual Studio rather than the .NET Framework?

The name 'foo' is not exist in namespace 'http://foo....'

I have an WPF solution and this solution consist of 3 project:
1-A project that has several WPF user control inside
2-Another project that has several WPF user control inside
3-A project which has Resources for 2 WPF projects above.
As you know, if you have common settings for you views like that
-Using Same FontFamily.
-Using same FontSize
-Using same FontWeight
-Using same BackroundBrush for all your User Controls etc.. You need to declare this setters in you all usercontrol tags like below:
<UserControl ....
FontFamily="{DynamicResource MyFontFamily}"
FontSize="{DynamicResource MyFontSize}"
FontWeight="{DynamicResource MyFontWeight}"
Background="{DynamicResource MyAppBgBrush2}"
Width="250" d:DesignHeight="350">
<Grid/>......
But I dont want to write same setters in all my UserControls. For thi reason, I decided to move this property setting in to a new c# file and locate it in Resource Project.
using System.Windows.Controls;
namespace Resources
{
public class PageBase : UserControl
{
public PageBase()
{
SetResourceReference(FontFamilyProperty, "MyFontFamily");
SetResourceReference(FontSizeProperty, "MyFontSize");
SetResourceReference(FontWeightProperty, "MyFontWeight");
SetResourceReference(BackgroundProperty, "MyAppBgBrush2");
}
}
}
So, In my Resource project, I adited AssemlyInfo.cs file like this:
[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.sat.com/winfx/2010/xaml/internalresources", "Resources")]
This edit gives me ability to declare/create a user control like below:
<internalresources:PageBase
xmlns:internalresources="http://schemas.sat.com/winfx/2010/xaml/internalresources">
<Grid>DoWhatEver<Grid/>
<internalresources:PageBase/>
From now, I do not have to create a usercontrol view which its tags start with
<UserControl...., I can start with <internalresources:PageBase......
My Question is that, VisualStudio 2010 can show me Design of all my user control bu Expression blend can not. Interesting part is that both in VS and Blend, my project compiling without any error But when I try to open my views in blend it says:
-The namespace 'PageBase' does not exist in namespace "http://schemas.sat.com/winfx/2010/xaml/internalresources"
P.S: References are added properly to my Project and My project was suitable to open with blend.
Inheriting your own UserControl makes sense when you are adding new abilities or properties to it. In your case you just want to override the design.
You can achieve this very simply by creating a Resource XAML (BasePageResources.xaml) and define your UI properties there with Setter tags.
<Style TargetType="Button">
<Setter Property="MinWidth" Value="75"/>
<Setter Property="MinHeight" Value="23"/>
<Setter Property="Margin" Value="11,11,0,0"/>
</Style>
You can dump all your setters in a single file, give your setters keys, just like CSS classes.
Then, in your App.xaml, you can include these files to make them Application-wide accessable.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BasePageResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
All your buttons in the application should now apply your styles. It's much better than inheriting, and WPF binding feels more natural than your work-around. Blend should cause you less problems if you design the way it expects you to.
I have found the problem. The problem is just about Expression blend. If your project setting does not have any PropertyGroup for Debug|AnyCPU you will get this problem. You should add this propertygroup to your csproj file like below via text editor :
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>

Categories