I want to access the Title value that is stored in the WMAppManifest.xml file from my ViewModel code. This is the same application title that is set through the project properties.
Is there a way to access this from code using something like App.Current?
Look at the source code for WP7DataCollector.GetAppAttribute() in the Microsoft Silverlight Analytics Framework. GetAppAttribute("Title") will do it.
/// <summary>
/// Gets an attribute from the Windows Phone App Manifest App element
/// </summary>
/// <param name="attributeName">the attribute name</param>
/// <returns>the attribute value</returns>
private static string GetAppAttribute(string attributeName)
{
string appManifestName = "WMAppManifest.xml";
string appNodeName = "App";
var settings = new XmlReaderSettings();
settings.XmlResolver = new XmlXapResolver();
using (XmlReader rdr = XmlReader.Create(appManifestName, settings))
{
rdr.ReadToDescendant(appNodeName);
if (!rdr.IsStartElement())
{
throw new System.FormatException(appManifestName + " is missing " + appNodeName);
}
return rdr.GetAttribute(attributeName);
}
}
This last answer seems overly complicated to me ; you could have simply done something like:
string name = "";
var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var customAttributes = executingAssembly.GetCustomAttributes(typeof(System.Reflection.AssemblyTitleAttribute), false);
if (customAttributes != null)
{
var assemblyName = customAttributes[0] as System.Reflection.AssemblyTitleAttribute;
name = assemblyName.Title;
}
I have used Michael S. Scherotter his excellent code sample to work it out to a fully working code sample:
using System.Xml;
namespace KoenZomers.WinPhone.Samples
{
/// <summary>
/// Allows application information to be retrieved
/// </summary>
public static class ApplicationInfo
{
#region Constants
/// <summary>
/// Filename of the application manifest contained within the XAP file
/// </summary>
private const string AppManifestName = "WMAppManifest.xml";
/// <summary>
/// Name of the XML element containing the application information
/// </summary>
private const string AppNodeName = "App";
#endregion
#region Properties
/// <summary>
/// Gets the application title
/// </summary>
public static string Title
{
get { return GetAppAttribute("Title"); }
}
/// <summary>
/// Gets the application description
/// </summary>
public static string Description
{
get { return GetAppAttribute("Description"); }
}
/// <summary>
/// Gets the application version
/// </summary>
public static string Version
{
get { return GetAppAttribute("Version"); }
}
/// <summary>
/// Gets the application publisher
/// </summary>
public static string Publisher
{
get { return GetAppAttribute("Publisher"); }
}
/// <summary>
/// Gets the application author
/// </summary>
public static string Author
{
get { return GetAppAttribute("Author"); }
}
#endregion
#region Methods
/// <summary>
/// Gets an attribute from the Windows Phone App Manifest App element
/// </summary>
/// <param name="attributeName">the attribute name</param>
/// <returns>the attribute value</returns>
private static string GetAppAttribute(string attributeName)
{
var settings = new XmlReaderSettings {XmlResolver = new XmlXapResolver()};
using (var rdr = XmlReader.Create(AppManifestName, settings))
{
rdr.ReadToDescendant(AppNodeName);
// Return the value of the requested XML attribute if found or NULL if the XML element with the application information was not found in the application manifest
return !rdr.IsStartElement() ? null : rdr.GetAttribute(attributeName);
}
}
#endregion
}
}
Only the first two answers are correct in scope of the original question. And the second is certainly not over complicated. Wrapping the helper method with a class for each possible attribute is good object orientated development and exactly what Microsoft do all over the framework, e.g. settings designer files generated by Visual Studio.
I'd recommend using the first if you just want one specific property, the second if you want more. Should be part of the SDK really. We're trying to read the WMAppManifest.xml here not the AssemblyInfo so standard assembly reflection metadata is no good.
By the way, if you really want to get the product name from the assembly attributes (not WPAppManifest.xml) then the last sample was reading the wrong attribute! Use the AssemblyProductAttribute not the AssemblyTitleAttribute. The assembly title is really the file title, by default the same as the assembly file name (e.g. MyCompany.MyProduct.WinPhone7App) whereas the product will typically be something like the properly formatted "title" of your app in the store (e.g. "My Product"). It may not even be up-to-date after using the VS properties page, so you should check that.
I use AssemblyInfo reflection for all other application types to show the official product name and build version on an about page, it's certainly correct for that. But for these special phone app types the store manifest has more importance and other attributes you may need.
The problem with all of those answers is that they have to read the file every single time it is accessed. This is bad for performance as there are battery issues to consider if you use it frequently. Koen was closer to a proper solution, but his design still went back to the file every time you wanted to access the value.
The solution below is a one-and-done read of the file. Since it is not likely to change, there is no reason to keep going back to it. The attributes are read as the static class is initialized, with minimal fuss.
I created this Gist to demonstrate.
HTH!
Related
We are using a 2sxc module on an DNN Evoq install, there are multiple instances of 2sxc module app on a page which are inserted in lot of pages.
Can we disable search from indexing the content of one particular 2sxc module through its template file using razor code?
Yes you can :)
It's a bit tricky but each razor can modify what / how something is indexed, this is often needed when indexing List/Details-pages. Here's the starting point in the docs: https://github.com/2sic/2sxc/wiki/Razor-SexyContentWebPage.CustomizeSearch
I would try the following (haven't tried it myself, but should work)
#functions
{
/// <summary>
/// Populate the search - ensure that each entity has an own url/page
/// </summary>
/// <param name="searchInfos"></param>
/// <param name="moduleInfo"></param>
/// <param name="startDate"></param>
public override void CustomizeSearch(Dictionary<string, List<ToSic.SexyContent.Search.ISearchInfo>> searchInfos, DotNetNuke.Entities.Modules.ModuleInfo moduleInfo, DateTime startDate)
{
// clear the search-infos
searchInfos["Default"] = new List<ToSic.SexyContent.Search.ISearchInfo>();
}
}
I have a table in sql server which stores some text in english (in nvarchar column).
I have to create a stored procedure to which I will pass a language (Hindi,Gujarati,Arabic) as a parameter & it will return me data converted to that language from English.
I understand that best way would be to store data in those languages in different columns but I cannot do it & want to rely on sql server.
Is there some utility or function which will help me accomplish this.
Looking For Starters or ideas..
Sql-Server cannot translate from one language to another language, but you can use external tools for that, please look at https://blogs.msdn.microsoft.com/samlester/2013/05/04/language-translation-in-sql-server-using-bing-translator-apis-sql-clr/
or you can create a wrapper for google translator: http://www.sqlservercentral.com/Forums/Topic819515-386-1.aspx
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
public static class Translator {
/// <summary>
/// Translates the text.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="languagePair">The language pair.</param>
/// <returns></returns>
public static void Main(string[] args)
{
TranslateText(args[1], args[2]);
}
/// <summary>
/// Translate Text using Google Translate
/// </summary>
/// <param name="input">The string you want translated</param>
/// <param name="languagePair">2 letter Language Pair, delimited by "|".
/// e.g. "en|da" language pair means to translate from English to Danish</param>
/// <param name="encoding">The encoding.</param>
/// <returns>Translated to String</returns>
public static string TranslateText(string input, string languagePair)
{
string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
string result = String.Empty;
using(WebClient webClient = new WebClient())
{
webClient.Encoding = System.Text.Encoding.UTF7;
result = webClient.DownloadString(url);
}
Match m = Regex.Match(result, "(?<=<div id=result_box dir=\"ltr\">)(.*?)(?=</div>)");
if (m.Success) result = m.Value;
return result;
}
}
but be carefull, Google will block you in an instant if you hit their servers with too many requests per minute.
DbSet<T>.Local provides an ObservableCollection that you can bind to WPF controls. In my case, I'm binding it to a grid.
If I was using view models, I would have them implement INotifyDataErrorInfo or IDataErrorInfo, and then write Fluent Validation validators to handle validation.
But here I'm binding to the models via DbSet<T>.Local. How should I handle validation?
Should I implement INotifyDataErrorInfo or IDataErrorInfo on my models?
Or is there some alternative?
If you are application is a thick client, meaning all layers of the application are on a single physical machine, then you should implement IDataErrorInfo on your models.
On the other hand, if your application is a thin multi-tired application, meaning that your model is implemented on a server and you are using the WPF desktop application to communicate with the server side code then you should implement INotifyDataErrorInfo.
Yes I think implement implementing IValidatableObject in POCO Models makes sense.
And you have probably noticed public ObservableCollection<TEntity> Local { get; }
There are several ways to do this. So a some homework.
As some background , might be useful
Then check this out
So we know EF has the concept of triggering Validations.
Configuration.ValidateOnSaveEnabled = true;
EF will also call IEnumerable<ValidationResult> ValidateInstance();
from interface IValidatableObject
if your entity POCO implements IValidatableObject
EF will trigger validate on SAve.
You can also easily trigger validations when you need to.
This all neatly links in with UoW concept.
public IEnumerable<DbEntityValidationResult> GetDbValidationErrors() { return
Context.GetValidationErrors(); } // Standard Context call get the problems
which can be used
catch (Exception ex) {....
var x = GetDbValidationErrors();
//....
So i think you are on the right track...
EDIT: Add SAMPLE POCOBase and demonstrate trigger validation.
public interface IFBaseObject : IValidatableObject {
// a POCO object must implement the VALIDATE method from IValidatableObject
bool IsValidInstance();
IEnumerable<ValidationResult> ValidateInstance();
}
public abstract class BaseObject : IFBaseObject {
// .... base object stuff removed....
/// <summary>
/// Get called every a Validation is trigger on an object. Here we return and Empty resultset to start with.
/// If you override ALWAYS call Base first and Continue to add your own results as desired.
/// Never fail to call base First !
/// </summary>
public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
# region Sample implementation for a POCO thats needs validation
/* Sample Implementation a sub type POCO might use.
var validationResult = base.Validate(validationContext).ToList();
if (true) // the condition that leads to a validation error
{
var memberList = new List<string> { "PropertyName" }; // the name of the offending property
var error = new ValidationResult("Error text goes here", memberList); // use teh textpool !!! no hardcoded texts
validationResult.Add(error);
}
return validationResult;
*/
# endregion
// now back in the base Class.
var validationResult = new List<ValidationResult>();
// hand back a list that is empty so errors if any can be added by SUBclasses
// we can check any Poco that implements a certain interface centrally.
var thisIsKeyGuid = this as IFKeyGuid;
if (thisIsKeyGuid != null) {
if (thisIsKeyGuid.Id == Guid.Empty) {
validationResult.Add(new ValidationResult("Id is required", new List<string>() {"Id"}));
}
}
return validationResult;
}
/// <summary>
/// Allows explicit triggering of Validation and returns a TRUE or false answer. Call anytime
/// </summary>
/// <returns></returns>
public virtual bool IsValidInstance() {
List<ValidationResult> vResults;
return SelfValidation(out vResults);
}
/// <summary>
/// Calls Self Validation which uses THIS object as the context for validation.
/// This means you can trigger a validation without first declaring a validation context.
/// IValidatableObject is effectively called for you. Witch causes "Validate" to be called
/// </summary>
/// <returns></returns>
public IEnumerable<ValidationResult> ValidateInstance() {
List<ValidationResult> vResults;
SelfValidation(out vResults);
return vResults;
}
/// <summary>
/// Although SelfValidation is defined at BaseObject level, this triggers the VALIDATION process on the current Object
/// see http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validator.aspx for mroe details
/// So if a POCO Object has overridden public virtual IEnumerable of ValidationResult Validate(ValidationContext validationContext)
/// then this method will be called. It should of course call :base.Validate
/// </summary>
/// <returns>true or false</returns>
private bool SelfValidation(out List<ValidationResult> vResults) {
var vc = new ValidationContext(this, null, null);
vResults = new List<ValidationResult>();
var isValid = Validator.TryValidateObject(this, vc, vResults, true);
return isValid;
}
}
I apologize in advance if my question is a bit vague.
I am looking into and learning reactiveui which I find a very interesting concept. For that, I am building a small application using WPF and Prism and I am having some difficulties understanding the way commands are build in the View Models.
I have an example which, if complete, will hopefully clarify things for me a little bit:
I would like to have a grid that displays some data. This data is loaded from a web service and my goal is to load it once when the view is shown to the user and then to reload it every x times (once a minute, for example) or when explicitly triggered from a button in the View.
So, my View Model currently looks like:
public class MyPageViewModel : ReactiveValidatedObject
{
/// <summary>
/// Collection of tickets
/// </summary>
public ReactiveCollection<TicketModel> Tickets { get { return _Tickets.Value; } }
/// <summary>
/// Private store of tickets
/// </summary>
private ObservableAsPropertyHelper<ReactiveCollection<TicketModel>> _Tickets { get; set; }
/// <summary>
/// The service which retrieves the tickets
/// </summary>
private ITicketService _ticketService;
public MyPageViewModel() : this(null) { }
public MyPageViewModel(ITicketService ticketService)
{
LoadTickets = new ReactiveAsyncCommand(null, 0);
}
/// <summary>
/// A command which exposes the load action for the tickets
/// </summary>
public ReactiveAsyncCommand LoadTickets { get; private set; }
private void loadTickets()
{
//returns a List<TicketModel>
var tickets = _ticketService.GetTickets();
}
}
My questions are:
1) How do I register an async command which will be fired every x times and will call the internal loadTickets function that will reload the Ticket storage? This command will also be fired by the exposed command LoadTickets.
2) In the function, loadTickets I will fetch each time a List from the service. How do I convert this list to the ReactiveCollection that is exposed to the UI.
1) How do I register an async command which will be fired every x times and will call the internal loadTickets function that will reload the Ticket storage? This command will also be fired by the exposed command LoadTickets.
Observable.Timer(TimeSpan.FromSeconds(10), RxApp.DeferredScheduler)
.InvokeCommand(LoadTickets);
2) In the function, loadTickets I will fetch each time a List from the service. How do I convert this list to the ReactiveCollection that is exposed to the UI.
// This makes it so when you invoke LoadTickets.Execute, Tickets
// gets updated.
// NB: Make loadTickets return a List<Ticket>
LoadTickets.RegisterAsyncFunc(x => loadTickets())
.Select(x => new ReactiveCollection<Ticket>(x))
.ToProperty(this, x => x.Tickets);
I have a WCF Data Service in my web app. I added a service reference using the "Add New Service Reference" command in my Silverlight application. I was looking at the Reference.cs file VS generates for me and noticed the setters don't check for a change before calling OnPropertyChanged. I'd like to change this behavior. Can I overrride the T4 template without having to override all the code generation?
If it's possible how would I go about doing it?
original generated code
/// <summary>
/// There are no comments for Property Title in the schema.
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string Title
{
get
{
return this._Title;
}
set
{
this.OnTitleChanging(value);
this._Title = value;
this.OnTitleChanged();
this.OnPropertyChanged("Title");
}
}
Desired change:
/// <summary>
/// There are no comments for Property Title in the schema.
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string Title
{
get
{
return this._Title;
}
set
{
// change to
if(this._Title != value) {
this.OnTitleChanging(value);
this._Title = value;
this.OnTitleChanged();
this.OnPropertyChanged("Title");
}
}
}
Unfortunately the Add Service Reference for WCF Data Services doesn't use T4 yet. So there's no easy way to do this. Feel free to vote for the feature here: http://blogs.msdn.com/b/astoriateam/archive/2010/09/10/what-do-you-want-to-see-added-changed-in-wcf-data-services.aspx