Providing the WPF designer with an image at DesignTime - wpf

This is a restatement of my question, the revision history contains the original mess.
What it boils down to is "How do I get the application's directory from my WPF application, at design time?"
Which duplicates the question here so if you happen to be passing by please vote to close, thanks.

Do you need the image to be "Content - Copy if newer"? If you switch it to "Resource" you can use the following path to reference the file:
"/MyImage.JPG"
or a longer version
"pack://application:,,,/MyImage.JPG"
given that the image is in the root of the project, otherwise just change the URI to
"/Some/Path/MyImage.JPG"
UPDATE 1:
For me, the longer pack uri syntax works with an image marked as "Content - Copy if newer" as well. However, the shorter syntax does not work. I.e:
This works:
"pack://application:,,,/MyImage.JPG"
This does NOT work:
"/MyImage.JPG"
I my example I added the image to the root of the project, and marked it as "Content". I then bound the design time data context to a view model with a property returning the longer pack URI above. Doing that results in the Content image being shown correctly at design time.
UPDATE 2:
If you want to load a bitmap source from a pack uri, you can do so by using another overload of the BitmapFrame.Create which takes an URI as the first parameter.
If I understand your problem correctly you get the string with the pack uri as the first item in the object array that is passed to your converter. From this string you want to load a BitmapSource.
Since the string contains a pack URI, you can create an actual URI from the string and then use that URI to load the BitmapSource:
var imagePath = values[0] as string;
// ...
try
{
var packUri = new Uri(imagePath);
BitmapSource bitmap = BitmapFrame.Create(packUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
{
// ...
}
}
}

Just return the exact path of the image from your entity in ImagePath property, such as ..
"C:\MyImage.JPG"
..
OR
..
"C:\MyApp\bin\Debug\MyImage.JPG"
Then your binding (i.e. <Image Source="{Binding ImagePath}" />) in .xaml will start working..

I solved it by leveraging the clevers found in this stackoverflow answer.
public class DMyViewModel : PhotoViewModelBase
{
public override string ImagePath
{
get
{
string applicationDirectory =
(from assembly in AppDomain.CurrentDomain.GetAssemblies()
where assembly.CodeBase.EndsWith(".exe")
select System.IO.Path.GetDirectoryName(assembly.CodeBase.Replace("file:///", ""))
).FirstOrDefault();
return applicationDirectory + "\\MyImage.JPG";
}
}
}

Related

Episerver -Validate content reference for image type

I have a content reference of type UIHint of image that accepts all the image types including .ico.
I need to validate this field so that the user can only upload file of type .ico
Right now even with the regex to only accept.ico file,the validation fails.
Could someone point out what is wrong with this.
I have validated the regex that should only accept.ico file but the validation fails
[Display(GroupName = Global.GroupNames.SiteSettings, Name = "Favicon", Description = "", Order = 20)]
[UIHint(UIHint.Image)]
[RegularExpression("[^\\s]+(.*?)\\.(ico)$", ErrorMessage = "Only .ico extension allowed")]
public virtual ContentReference Favicon { get; set; }
Kindly guide me in the right direction
Since .ico files probably aren't intended to be rendered on the site like other images (?) I'd create a separate content type called IconImage for the .ico file extension and then use an AllowedTypes attribute on your ContentReference property, specifying IconImage as the only allowed type.
If you already have a lot of icon files uploaded, i.e. mapped to an existing content type shared with other types of images, I'd probably create a separate validator (class implementing IValidate<T>) for applicable content type(s) to validate the ContentReference property.

setting URL as mediaElement source

I keep getting an "object reference not set to an instance of an object" and i have absolutely no idea why!
MediaElement1.Source = New URI(trackstream(0), UriKind.Absolute)
If i mouseover to check everything it seems fine, that variable position contains a direct link to an mp3, It even says if i mouseover it that the Source has been set to that URL but then i get this error.
i am just trying to set url as the first in a list that i have downloaded from a textfile previously, then put into that array. I have tried changing the urlkind and omitting it.
This works:
public MainWindow()
{
string[] trackstream = new string[] { #"C:\Users\Public\Music\Sample Music\sound.wma" };
InitializeComponent();
media.Source = new Uri(trackstream[0], UriKind.Absolute);
}

How to display application title in XAML text field

I have a Windows Phone page code that is shared by multiple applications.
At the top of the page, I show the title of the application, like so:
Is it possible to make the text be bound to the application title as defined in the application's assembly?
I realise that I could do this by code by reading the title in the assembly and then doing something like:
this.ApplicationTitle.Text = title;
I was hoping that the title as defined in the assembly could be accessed with some magic like:
Text={assembly title}" directly from within the xaml.
Thanks
Create a property called ApplicationTitle that returns that name of the application like the following, and then bind to it in XAML:
public string ApplicationTitle
{
get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; }
}
(You can use a relative binding source if you can't or don't want to use the data context.)
edit:
I just realized that my method involved security considerations since GetName is a method that is [Security Critical]. And I got a MethodAccessException stating: Attempt to access the method failed: System.Reflection.Assembly.GetName()
So here's another way to get the assembly name and return it in a property by using the AssemblyTitle attribute.
public string ApplicationTitle
{
get
{
System.Reflection.AssemblyTitleAttribute ata =
System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(
typeof(System.Reflection.AssemblyTitleAttribute), false)[0] as System.Reflection.AssemblyTitleAttribute;
return ata.Title;
}
}
To bind in XAML, you can use this:
Text="{Binding ElementName=LayoutRoot, Path=Parent.ApplicationTitle}"

Silverlight InlineCollection.Add(InlineUIContainer) missing?

I'm having difficulty adding the inline of specific type InlineUIContainer into the InlineCollection (Content property) of a TextBlock. It appears the .Add() method of InlineCollection doesn't accept this type, however you can clearly set it through XAML without explicitly marking the content as a InlineContainer, as demonstrated in many examples:
http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer.aspx
Is it possible to programatically add one of these as in the following?
Target.Inlines.Add(new Run() { Text = "Test" });
Target.Inlines.Add(new InlineUIContainer() {
Child = new Image() { Source = new BitmapImage(new Uri("http://example.com/someimage.jpg")) } });
Target.Inlines.Add(new Run() { Text = "TestEnd" });
I have a feeling what's going on is that Silverlight is using a value converter to create the runs when specified in XAML as in the example which doesn't use InlineContainer, but I'm not sure where to look to find out.
The specific error I'm getting is as follows:
Cannot add value of type 'System.Windows.Documents.InlineUIContainer' to a 'InlineCollection' in a 'System.Windows.Controls.TextBlock'.
As pointed out by Jedidja, we need to use RichTextBox to do this in Silverlight.
You can't Add() Runs directly, but you can add Spans containing Runs.
Interestingly, you can also do this:
textBlock.Inlines.Clear();
textBlock.Inlines.Add(new Span());
textBlock.Inlines[0] = new Run();
Not that it's a good idea to hack around what the framework is actively trying to prevent you from doing.
P.S. If you can't figure out what XAML is doing, inspect the visual tree.

Creating WPF ResourceDictionary from code doesn't seem to work when setting ResourceDictionary.Source

I have a project containing a xaml ResourceDictionary that I wish to use outside of a FrameworkElement. The resource dictionary will contain a DataTemplate for a class local to the project to avoid polluting the app.xaml (as the project is a prism module, and will not always be present depending on config).
So, I have a test.xaml file with a Resource build action.
This is intended to supply the DataTemplate for a TestObject class.
In the TestObject class I have a GetTemplate() method
The following works:
DataTemplate GetTemplate()
{
Uri uri = new Uri("MyProject;component/test.xaml", UriKind.Relative);
var dict = new ResourceDictionary { Source = uri};
return (DataTemplate)dict["TestObjectDataTemplate"];
}
This throws an exception when I assign the uri to the ResourceDictionary.Source property
DataTemplate GetTemplate()
{
Uri uri = new Uri("/test.xaml", UriKind.Relative);
var dict = new ResourceDictionary { Source = uri};
return (DataTemplate)dict["TestObjectDataTemplate"];
}
The second example fails as the /test.xaml can't be found in the local assembly. Why would I need to access it with "ReferencedAssembly;component/test.xaml" ?
In this instance, does local assembly mean the executing assembly or the assembly the code/resource is part of?
Edit: Updated to reflect the actual issue.
Try UriKind.RelativeOrAbsolute.
More clearly like.
DataTemplate GetTemplate()
{
ResourceDictionary resource = new ResourceDictionary()
{
Source = new Uri(#"/AssemblyFullName;component/test.xaml", UriKind.RelativeOrAbsolute)
};
return (DataTemplate)resource["TestObjectDataTemplate"];
}
Edit:
In this instance, does local assembly
mean the executing assembly or the
assembly the code/resource is part of?
Say for example:
You have two projects Project A and Project B.
You are using Project A as reference in Project B
Now, if you want to use the resource like this /test.xaml. Then, this resource should reside in the Project B. Since, it is the executing assembly. [It will be available for both Project A as well as Project B. You could use the above mentioned syntax. like /test.xaml]
If you want the resource to be defined and used inside Project A. Then, you should use "/ProjectA;component/test.xaml" because it is not the current executing assembly. [It will be available for both Project A as well as Project B. You have to use "/ProjectA;component/test.xaml" this to access in both the projects]
Setting the Source attr works, I successfully used it in many projects.
Your Uri might be wrong. You should try a fully qualified pack Uri, like :
dict.Source = new Uri("pack://application:,,,/test.xaml");
If your test.xaml file is not in the project root, be sure to set its path correctly.

Resources