To optimize my application, I create a SharedResourceDictionary. With this, I save about 250 mb of memory at run-time, so it works well.
My problem is in design-time, in Blend 4, I have no more access to my resource. To modify a template (witch is in my resource file), I usually right click on my control and I choose Edit Template --> Edit Current, like in this image:
I need to modify my template that way, it's 100x faster then to go in my resource file and find the good one... I have A LOT of resources...
My SharedResourceDictionary is implemented like this (found on the web):
public class SharedResourceDictionary : ResourceDictionary
{
/// <summary>
/// Internal cache of loaded dictionaries
/// </summary>
public static Dictionary<Uri, ResourceDictionary> _sharedDictionaries =
new Dictionary<Uri, ResourceDictionary>();
/// <summary>
/// Local member of the source uri
/// </summary>
private Uri _sourceUri;
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get
{
if (IsInDesignMode)
return base.Source;
return _sourceUri;
}
set
{
_sourceUri = value;
if (!_sharedDictionaries.ContainsKey(value))
{
// If the dictionary is not yet loaded, load it by setting
// the source of the base class
base.Source = value;
// add it to the cache
_sharedDictionaries.Add(value, this);
}
else
{
// If the dictionary is already loaded, get it from the cache
MergedDictionaries.Add(_sharedDictionaries[value]);
}
}
}
/// <summary>
/// Check if we are in Blend to prevent error
/// </summary>
public bool IsInDesignMode
{
get
{
return
(bool)
DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
typeof(DependencyObject)).Metadata.DefaultValue;
}
}
}
Someone have an idea if there is a solution for this? I really want to keep this shared dictionary due to the memory improvement, but I also want to modify my resource easily...
Any clue will be appreciated. Thank you.
EDIT:
Doing this (SharedResourceDictionary), I also have an issue with static resource:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
And when I change them to dynamic resource it works, or if I change the SharedResourceDictionary by a simple ResourceDictionary it also works. The project can compile but I cannot edit the resource without a modification as I said.
Are you compiling for .NET 4?
You might be hitting the bug where it fails to scan your ResourceDictionaries properly.
There's talk that you have to add your ResourceDictionaries at the the App.xaml level (in Application.Resources) level, and add a dummy default style.
This might not work for you as you seem to be adding your resources in your control.
http://blogs.windowsclient.net/rob_relyea/archive/2010/04/26/my-staticresource-reference-worked-differently-with-wpf-4-rc-than-it-does-with-wpf-4-rtm.aspx
Adding a Merged Dictionary to a Merged Dictionary
Trouble referencing a Resource Dictionary that contains a Merged Dictionary
http://connect.microsoft.com/VisualStudio/feedback/details/553528/defaultstylekey-style-not-found-in-inner-mergeddictionaries
Actually it is problematic to use this feature. =(
Maybe in the future things will get better, do not know if will help, but you can try a different implementation:
WPF SharedResourceDictionary
(but do not guarantee it will work)
I gave up using the SharedResourceDictionary because of problems at design time, only when the project is 100% completed I'll be back to use.
Related
I am trying to set up an auto-versioning update on TortoiseSVN for a project I'm working on. It's very straightforward; the code is supposed to pull the SVN revision and display the date & location whenever anyone does a checkout/update from the repo.
Below I have a template class
public static class SvnRevision
{
/// <summary>
/// SVN repo revision
/// </summary>
public const string REVISION = "$WCREV$";
/// <summary>
/// SVN repo location
/// </summary>
public const string REPOSITORY_URL = #"$WCURL$";
/// <summary>
/// SVN repo date
/// </summary>
public const string REPOSITORY_DATE = "$WCDATE$";
}
I want to know the background steps necessary to get this program to work. Appreciate if anyone could provide a walk through or tutorial. I am trying to do this on Visual Studio 2022.
I made commits using this class to SVN without any luck, a search indicates that I need to set up versioning and provide some kind of source/destination files that I don't know how to do. Below is a link I've referenced.
https://docs.huihoo.com/tortoisesvn/1.5.7-en/tsvn-subwcrev-keywords.html
Before selecting The Right Thing (tm) for your task and|or using SubWCRev (which you try to use, according to used subwcrev-specific keywords) you have to know some things
Best (and, really, single correct place) for reading updated, relevant to latest release docs about SubWCRev is TortoiseSVN website (SubWCRev is part of TortoiseSVN distribution and is not of pure SVN per se)
"...whenever anyone does a checkout/update from the repo" such person with WC of your repo have more natural way to know, which revision he|she got and whence: ordinary svn info tells it, instead of reading your code or compiling it in order to see message-box with collected data
Contrary to SVN-keywords, which work from a box (when inserted into files) and updated automagically on the fly, subwcrev's keywords have to have "release" stage (read docs): you insert keywords in template-file, which, after processing by SubWCRev, will produce new file, in which template-placeholders are replaced by data, actual only for this moment and not updated automatically after WC-changes (you must to refresh files from templates again)
In your case you can
move public static class SvnRevision into somename.cs.tpl with all needed keywords
Before all other steps in your compile-chain add call of SubWCRev, smth.like SubWCRev.exe path\to\WC somename.cs.tpl somename.cs
In you code use references to somename.cs, which also have to be in svn-ignore and not versioned, while template have to be versioned
HTH
Thanks for all the help everyone. Likely my line of questioning was not clear or I worked it wrong, but the problem was that I needed to set a command line as a prebuild event so that the SVN keywords would update.
Prebuild event command line: "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "$(ProjectDir)." "$(ProjectDir)SvnRevision_Template.cs" "$(ProjectDir)SvnRevision.cs"
Is it possible to have resource files in separate folders for each language as shown below? I'd like all my English files in an "en" folder and all my Spanish files in an "es" folder. I have tried using the resources with both files in the main Resources folder (no subfolders), and it works correctly. However, when I have the files in separate "en" and "es" folders like in the image, I always get the English text even if my browser is set to display Spanish.
I've included file properties for each resource file along with the code I'm using to retrieve the value
var value = Language.Resources.ResourceManager.GetString(resourceName.Trim());
The auto-generated Resources.Designer.cs file includes the following code for the ResourceManager property. Is that first parameter "ClientInterface.Resources.en.Resources" the reason why it's always giving me English?
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ClientInterface.Resources.en.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
I see it like you need to write single Resource class manually and Custom Tool for Resources should be deleted.
And you need to write your own logic for receiving right ResourceManager.
This small sample without optimizations, but you can start with this.
The auto-generated Resources.Designer.cs file includes the following
code for the ResourceManager property. Is that first parameter
"ClientInterface.Resources.en.Resources" the reason why it's always
giving me English?
It's mostly because in autogenerated resource it is loading only english resource, but you need to write your logic, how to load your resource.
Example NamespaceToResources.Language.Resources
Or NamespaceToResources.Culture.Resources
In the next sample I'm loading Resource for current Culture
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
return new global::System.Resources.ResourceManager($"ClientInterface.Resources.{Thread.CurrentThread.CurrentUICulture.Name.Replace("-", "_")}.Resource", typeof(Resource).Assembly);
}
}
I am just starting to use the ToolStrip object on my windows Form.
I followed a nice tutorial that explained how to add the tool strip, then add a button, then assign a image to the button.
So, I added the toolstrip and then added the button:
As you can see, in the above screen shot I have also tried importing the image. I did this by right-clicking the button and choosing Set Image:
When I clicked OK, these extra files (MyGenioViewer1.Designer.cs) were automatically generated:
But now the project will not compile. If I restore my previous backup and do everything except importing a button, it compiles.
These are the errors:
Severity Code Description Project File Line Suppression State
Error CS0111 Type 'MyGenioView' already defines a member called '.ctor' with the same parameter types GENIO Viewer D:\My Programs\GENIO Viewer\GENIO Viewer\MyGenioView.cs 65 Active
Error CS0262 Partial declarations of 'MyGenioView' have conflicting accessibility modifiers GENIO Viewer D:\My Programs\GENIO Viewer\GENIO Viewer\MyGenioView.Designer.cs 25 Active
Error CS0260 Missing partial modifier on declaration of type 'MyGenioView'; another partial declaration of this type exists GENIO Viewer D:\My Programs\GENIO Viewer\GENIO Viewer\MyGenioView1.Designer.cs 25 Active
Error CS0111 Type 'MyGenioView' already defines a member called '.ctor' with the same parameter types GENIO Viewer D:\My Programs\GENIO Viewer\GENIO Viewer\MyGenioView1.Designer.cs 32 Active
Error CS0121 The call is ambiguous between the following methods or properties: 'MyGenioView.MyGenioView()' and 'MyGenioView.MyGenioView()' GENIO Viewer D:\My Programs\GENIO Viewer\GENIO Viewer\GENIO_Viewer_Form.cs 53 Active
Clearly, the addition of these extra files has caused me a problem. I don't know why they were added. I don't know how to resolve the issue. And I don't know how to prevent it.
Thank you for any clarification about resolving this issue correctly.
The original MyGenioView.Designer.cs contains:
namespace GENIO_Viewer
{
partial class MyGenioView
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// MyGenioView
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Name = "MyGenioView";
this.ResumeLayout(false);
}
#endregion
}
}
The new MyGenioView1.Designer.cs causing issues is:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace GENIO_Viewer {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class MyGenioView {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal MyGenioView() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("GENIO_Viewer.MyGenioView", typeof(MyGenioView).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
}
}
You've got a real mess here. Somehow, you wound up with a Designer file separate from a Form. One that, in fact, doesn't look like a designer file at all. What I didn't see in your screenshot was the form to which those Designers are supposed to be linked.
It should look like this:
You didn't show this class's code: MyGenioView.cs, but I'm guessing it already defines at least some of what is in MyGenioView1.Designer.cs.
The bottom line is that you have too many conflicting (partial) classes in too many places. It looks to me like you should have one class (code page) for MyGenioView, and another for the form. Anything else is conflicting.
P.S. I also wonder if you are confusing WPF constructs with WinForms. They are vastly different. Your use of the word 'View' suggested this to me. I may be wrong.
I found a couple of suggestions on this question but I can't get it on my problem.
We got a .ddl which replaces placeholders in a word file and returned a memorystream. This works fine in a deliverd Test Application with a WPF FrontEnd.
Now we need this solution in a CRM2011 context. I added a reference to this .dll file in my CRM Project, build the logic exactly the way as seen in the example and boom a MissingMethodException appears.
I debuged to the point where the Exception is thrown and found somethine like this:
readonly Dictionary<Type, object> typeMap = new Dictionary<Type, object>();
/// <summary>
/// Returns an instance of the DataService implementing the <typeparamref name="TService"/> interface
/// </summary>
/// <typeparam name="TService">type of the interface for the DataService</typeparam>
/// <returns></returns>
public TService For<TService>()
{
if (typeMap.ContainsKey(typeof(TService)))
{
object value = typeMap[typeof(TService)];
if (value is Type)
{
return (TService)Activator.CreateInstance((Type)typeMap[typeof(TService)]);
}
return (TService)value;
}
return Activator.CreateInstance<TService>();
}
The line Activator.CreateInstance(); throws the Exception. I have absolutely no idea what is going wrong here and why this piece of code works fine on the test app.
I'm assuming it's because you are either attempting to create a Static class, or a class without an empty constructor. Add try catch that displays the type that is attempting to be created to narrow down what is happening.
I incorporated a WLAN API given from Codeplex's Managed WiFi site into my C# project (Windows Form Application). In the API, different functions are given to retrieve various aspects of the machine's current WiFi profile. I am only interested in retrieving the RSSI strength given in the function below. I then want to take that value and stick it in a text box on my form.
(Visual Studio 2008)
In WlanAPI.cs file, the function I am interested in exists as such:
namespace NativeWifi
{
public class WlanClient
{
/// <summary>
/// Represents a Wifi network interface.
/// </summary>
public class WlanInterface
{
/// <summary>
/// Gets the RSSI.
/// </summary>
/// <value>The RSSI.</value>
/// <remarks>Not supported on Windows XP SP2.</remarks>
public int RSSI
{
get
{
return GetInterfaceInt(Wlan.WlanIntfOpcode.RSSI);
}
}
In myApp.cs I have a textbox simply named 'wifi' that will display the current RSSI.
I have included : 'using NativeWifi' in myApp.cs header, but can't seem to get the data from the RSSI function in the WlanAPI.csproj. The project builds and compiles just fine. I'm just snagged on getting the RSSI value.
In myApp.cs I have a statement to the effect of:
wifi.Text = (GetInterfaceInt(Wlan.WlanIntfOpcode.RSSI)); //app form txt_box=RSSI value
I know this is incorrect, but shows what I am trying to do.
Any ideas?
Thanks.
You should be able to solve the problems you are facing by
Adding a reference to WlanAPI.dll or WlanAPI project (if you add it to your solution)
Using code like following:
Using NativeWifi;
Class MyAPP : Form
{
public void PrintRSSI()
{
WlanClient client = new WlanClient();
var interfaces = client.Interfaces;
//Now chose an interface out of all the available interfaces. Usually there would be zero or 1 interfaces available
if(interfaces.Length > 0)
{
//Select first available interface. A more complicated logic can present the list of available interfaces to the user and and then display RSSI for the selected interface
wifi.Text = interfaces[0].RSSI.ToString();
}
}
//Other code for the class
}