When I'm using the publish option in Visual Studio to create an .*exe package of my application, I can change the Publish Version and it starts from 1.0.0.0.
Every time I'm changing something and re-publish it, the version number is increment automatically, and I want to bind that property to a TextBlock in my WPF.
How can I do that?
Thank you.
First of all you have to reference in your project to System.Deployment.dll than you can try this code:
public string PublishVersion
{
get
{
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
Version ver = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
return string.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision);
}
else
{
return "Not Published";
}
}
}
Remember that will be work only when you have installed your app.
Related
I've attempted to start playing with .Net MAUI and I've setup my development environment following the steps as described in:
https://learn.microsoft.com/en-us/dotnet/maui/get-started/first-app?pivots=windows
https://learn.microsoft.com/en-us/windows/apps/project-reunion/set-up-your-development-environment#required-workloads-and-components
https://learn.microsoft.com/en-us/xamarin/android/get-started/installation/android-emulator/device-manager?tabs=windows&pivots=windows
I've also run the 'maui-check' CLI tool and everything checks out, but when I create a new .NET MAUI App with Visual Studio 2019 v16.11.0 Preview 2.0 (running on Windows 10 Home 20H2), I get the 'The name 'InitializeComponent' does not exist in the current context' build errors. It also doesn't find the references to any controls on the form e.g. 'The name 'CounterLabel' does not exist in the current context'
I've tried almost everything in this post The name 'InitializeComponent' does not exist in the current context which contains suggestions like adding and removing files, making changes and changing them back... basically everything except throwing a penny in a wishing well.
I found that a common mistake is a namespace mismatch, but here is what I have showing that the namespaces are correct:
App.xaml:
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
x:Class="MauiApp1.App">
...
</Application>
App.xaml.cs
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using System;
using Application = Microsoft.Maui.Controls.Application;
namespace MauiApp1
{
public partial class App : Application
{
public App()
{
InitializeComponent(); <-- This is throwing the build error...
}
protected override IWindow CreateWindow(IActivationState activationState)
{
this.On<Microsoft.Maui.Controls.PlatformConfiguration.Windows>()
.SetImageDirectory("Assets");
return new Microsoft.Maui.Controls.Window(new MainPage());
}
}
}
MainPage.xaml:
ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
...
</ContentPage>
MainPage.xaml.cs
using System;
using Microsoft.Maui.Controls;
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent(); <-- This is throwing the build error...
}
int count = 0;
private void OnCounterClicked(object sender, EventArgs e)
{
count++;
CounterLabel.Text = $"Current count: {count}"; <-- This is throwing the build error...
}
}
}
Any help will be greatly appreciated!
---=== UPDATE ===---
The path to the project I created is c:\develop\c#...... as soon as I copy the project to a folder that doesn't contain 'c#' it works. This clearly causes some parsing in the background to fail.
I faced the same issue and looks like when I created a ContentPage in VS its still pointing to Xamarin Forms. After changing namespace to MAUI, I updated the Build Action(RightClick on Xaml Page>>Properties>>BuildAction) of XAML Page to MauiXaml and it worked for me.
Close VS2022
Open VS2022
Open project with menu VS2022(File - Open - Project...)
What caused it for me was that I had renamed the xaml and xaml.cs file something else but I hadn't updated it in the ContentPage node in the xaml under x:Class
The same error popped in for me being an absolute beginner and missing the ending '>' in the XAML file. So, It could also be XAML errors, which leads to this error.
The path to the project I created is c:\develop\c#...... as soon as I copy the project to a folder that doesn't contain 'c#' it works. This clearly causes some parsing in the background to fail.
A very simple answer but has happened to me a few times.
Make sure you are using the preview version of VS 2022. It is very easy to accidently open up any other version of VS, which will cause the error to occur.
I have found a workaround for this problem. I am a newbie to c# and especially to .net Maui therefore, my apologies if I misinterpreted things differently.
First things first The error is about the context that the InitializeComponent() resides in. Context is nothing but the nuget package dependency that the file is looking for.
To change the context
Open the .cs file and at the top of the editor click the navigation bar as shown in the
Image
Change the context to Windows specific dependency from the list.
And also the other workaround would be to change the build action of the xaml file to MauiXaml as others mentioned
I'm using VS 2022 Preview 17.4.0 Preview 1.0, I don't know why but when you create a new Content Page it creates with the wrong namespace. take a look in the C# file and fix the namespace. It worked for me.
I am using preview version of 17.5 VS2022, I add to do below changes to run the app after adding new page.
*.Xmal Build Action -> BundleResource
*.Xmal.cs Build Action -> Compile
Setup: Unity 2017.4.16f1 / 2018.2.18f1
I've got an own written C# library, using .Net. The library works fine in a Winforms application. In general it contacts a server application and fetches some data.
Now we want to visualize the data within Unity. For that I followed this guide: https://docs.unity3d.com/Manual/UsingDLL.html. In short: Drag and drop the library in the asset folder, add a new script and access the library. My first problem was the compatibility. Because, I used Unity 2017.4.16f1 first, I needed to downgrade the library from .Net 4.7.2 to .Net 4.6 and change the project settings to the same .Net framework. After getting rid of all the compiler errors, I ran into my current Problem.
I attached Visual Studio 2017to unity set up my breakpoint and started the app within Unity. The breakpoint is on the first call of a function from my custom library. The breakpoint is reached. But if i say 'Step over' (either via click or F10) the active line doesn't switch and I get back to Unity. It's like i never started debugging. Unity goes on like nothing happened.
After that i tried it with Unity 2018.2.18f1. However, there is the same problem. The library isn't called.
Regarding some compatibility issues, I built the Unity project. No error occurs. Finally I stripped down the external library to a basic level. But again I got this strange behavior. Below is the script for unity and an example class from the custom library. Thank you for every advice.
C#:
[Serializable]
public class PostgreSQLParameters
[XmlElement(ElementName = "PostgreSQL_User", IsNullable = false)]
public string UserName
{
get
{
return this.userName;
}
set
{
if (this.userName == value)
return;
this.userName = value;
this.OnPropertyChanged();
}
}
public PostgreSQLParameters()
{ }
Unity:
void Start () {
string cwd = Directory.GetCurrentDirectory();
string pathVariable = Environment.GetEnvironmentVariable("PATH");
try
{
//this line has the break point, untill here all is ok
//after the Debug steps in, I hit F10 here to get to the next line,
//which should be the bracket befor the catch
PostgreSQLParameters parameter = new PostgreSQLParameters();
} // The cursor should be here after hitting F10, but disappears only
catch (Exception)
{
throw;
}
}
The core problem are the dependencies. I compared the dependencies in my library, with them from the unity project within Visual Studio. It turns out on library wasn't linked in Unity: System.Data.DataSetExtension. There are two solutions:
A) If you don't need them, omit them in your library. Delete the reference in the library and compile again
B) Or if you need them and it's part of '.Net' do the following (not tested):
See https://learn.microsoft.com/en-us/visualstudio/cross-platform/unity-scripting-upgrade?view=vs-2017#adding-assembly-references-when-using-the-net-4x-api-compatibility-level
add a file, called mcs.rsp, in the asset root
Insert the line -r:System.Data.DataSetExtension (take the name of the reference you need)
Restart Unity
whenever am trying to access the value of a property through reflection am getting a error like
"System.ArgumentException: Get Method not found for 'XXXX' at
System.Reflection.MonoProperty.Get"
. Am using Xamarin studio version 6.3 and property also declared as public only.
Model:
using System;
namespace XXXX.Core
{
public class ManufacturingParameters
{
public ManufacturingParameters()
{
}
private int oemCode;
public int OEMCode
{
get { return oemCode; }
set { oemCode = value; }
}
}
}
Ex Code:
ValueString = deviceInfoContentVC.devInfo.manufacturingParameters.GetType().GetProperty(ParamSchema.Key).GetValue(deviceInfoContentVC.devInfo.manufacturingParameters).ToString();
The above code works perfectly in Visual studio but not in xamarin studio.
But I can access the same property through below code:
ValueString = deviceInfoContentVC.devInfo.manufacturingParameters.oemCode.ToString();
Since i wanted to access it dynamically i need reflection kind of mechanism.
Is there any alternative solution/fix for this?
Thank You.
Finally resolved this issue and here is the solution I found.
It Was the linker stripping it away. I had changed the Linker Behaviour to "Link Framework SDK's Only" from "Link All" under iOS project's settings, and under the Build heading.
Now its works like a charm.
I am trying to get Visual Studio 2015 (14.0) to use auto properties when implementing an interface using refactoring for C#.
I.e. I want this;
public object SomeProperty { get; set; }
as opposed to this;
public object SomeProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
I have accomplished this in past versions of Visual Studio by editing the code snippet file (instructions here) but I cannot get this to work using Visual Studio 2015.
Ok, so I stumbled upon the answer during my testing of VS2019 Preview (16.0).
In the main menu bar Tools --> Options --> Text Editor --> C# --> Advanced look for the option Implement Interface or Abstract Class under When generating properties choose prefer auto properties.
This results in the same outcome that snippets used to take care of pre VS2015.
You can solve by editing the PropertyStub.snippet
Just go to C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring open PropertyStub.snippet and edit:
$GetterAccessibility$ get
{
$end$throw new $Exception$();
}
$SetterAccessibility$ set
{
throw new $Exception$();
}
to
$GetterAccessibility$ get;
$SetterAccessibility$ set;
I've added the Windows Azure Cache 1.8.0 nuget package to my solution, but it ends up crashing Visual studio when I load the project. I've found that I can "prevent" the crashing by removing the dlls from the bin folder, and then again when visual studio adds them back to the bin while the project loads.
The dlls I'm removing are:
Microsoft.ApplicationServer.Caching.AzureClientHelper.dll
Microsoft.ApplicationServer.Caching.AzureCommon.dll
Microsoft.ApplicationServer.Caching.Client.dll
Microsoft.ApplicationServer.Caching.Core.dll
When I look at the event viewer for the visual studio crash I get this:
Application: devenv.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentNullException
Stack:
at System.Threading.Monitor.Enter(System.Object)
at Microsoft.ApplicationServer.Caching.DataCacheFactory.Close()
at Microsoft.ApplicationServer.Caching.DataCacheFactory.Finalize()
I'm uncertain why VS is doing things with the dlls while the project is loading, but I admit I'm not an expert on that.
I've basically followed the process described on this page to add a dedicated cache worker role for caching:
http://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/
I've tried removing and reinstalling the package, removing and reinstalling the Visual Studio SDK (Oct 2012), but the problem comes back.
Also, I don't have the App Fabric Server installed.
Thanks in advance for your help!
In case anyone else runs into this problem, I'm providing what seemed to work for me here.
In order to get visual studio to load the project, I removed the DLLs from the project. I also removed them as the project was loading and VS put the dlls back in the bin folder.
I removed the references to the dlls. Then I removed my code that was using the datacachefactory.
In the end I believe that it was caused by an improper use of the cache in my code that I had performed a build on. I was able to correct the usage of it, build the solution and get all the dlls back into the project.
Previously by datacache object had not be static.
here's my correct usage of the datacache factory:
using System;
using Microsoft.ApplicationServer.Caching;
namespace WebRole1.Classes
{
public class AzureCache
{
public static DataCache cache { get; set; }
public static DataCacheFactory dataCacheFactory { get; set; }
public AzureCache()
{
if (cache == null){
DataCacheFactoryConfiguration cfg = new DataCacheFactoryConfiguration();
cfg.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, "CacheWorkerRole1");
dataCacheFactory = new DataCacheFactory(cfg);
cache = dataCacheFactory.GetDefaultCache();
}
}
public void Add(string item, object value)
{
cache.Add(item, value);
}
public void Add(string item, object value, TimeSpan timeout)
{
cache.Put(item, value, timeout);
}
public object Get(string item)
{
return cache.Get(item);
}
public TimeSpan TimeRemaining (string item)
{
DataCacheItem DCitem = cache.GetCacheItem(item);
return DCitem.Timeout;
}
public void Flush()
{
cache.Clear();
DataCacheFactory cacheFactory = new DataCacheFactory();
cache = cacheFactory.GetDefaultCache();
}
}
}
We may need to capture a dump of the visual studio process(devenv.exe) to see what is causing this exception. You can use debugdiag("http://www.microsoft.com/en-us/download/details.aspx?id=26798") to capture the dump.
You may need to involve Microsoft support services("http://www.windowsazure.com/en-us/support/contact/") to further investigate this since the exception is coming from the cache code itself.
It's not an issue exclusively related to Visual Studio. I get it while running my webrole.
Check out this other article.