I've added an icon to my WPF project (BuildAction set to Resource) and I'm now trying to load that icon into a stream:
using(Stream iconStream = Application.GetResourceStream(new Uri("red.ico")).Stream)
{
// use the stream
}
This gives me the following error:
Invalid URI: The format of the URI could not be determined.
I've tried altering the Uri construction to include UriKind.Relative. This gives:
Cannot locate resource 'red.ico'.
I've looked at various articles on this For Example (from SO) and I can't see what I'm doing wrong.
Any help greatly appreciated.
Try something like new Uri("pack://application:,,,/red.ico"), see this page for more info on URIs in WPF.
Ultimately my issue boiled down to the fact that I had created a custom entry point for my WPF application and I was attempting to create a URI before the Application's static constructions had been called.
The code changed from something like this:
public static void Main()
{
var myUri = new Uri("/red.ico", UriKind.Relative);
var app = new AppMain();
app.Run();
}
to something like this:
public static void Main()
{
var app = new AppMain();
var myUri = new Uri("/red.ico", UriKind.Relative);
app.Run();
}
Related
Hi I have a WinForms project targetting .net core. I am trying to load an image from a URL, but all I am getting is this:
Inside a UserControl I have added a PictureBox (pic1) and a Button.
Added the following as an example to the click event of the button:
pic1.LoadAsync("https://images.pexels.com/photos/4815143/pexels-photo-4815143.jpeg");
also
pic1.ImageLocation = "https://images.pexels.com/photos/4815143/pexels-photo-4815143.jpeg";
Have tried with different URLs
Any ideas what is wrong?
Edit
I've tried an alternative option, something seems to be getting stuck on DownloadData
using (WebClient webClient = new WebClient())
{
byte[] data = webClient.DownloadData("https://images.pexels.com/photos/4815143/pexels-photo-4815143.jpg");
using (MemoryStream mem = new MemoryStream(data))
{
using (var yourImage = Image.FromStream(mem))
{
}
}
}
I'll be the first to admit I'm no expert but the plugin API offers me no information for my issue.
I have the following code which is meant to set an image source into an image tag in my xaml called Controller2 when the Down button is pressed on the keyboard:
public bool OnDown(bool held)
{
string path = $"pack://siteoforigin:,,,/Themes/Test/Images/Controls/Atari 2600/Joystick.png";
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(path, UriKind.Absolute);
bitmap.EndInit();
Controller2.Source = bitmap;
}
And I'm getting a Not all code paths return a value error for OnDown.
The plugin API is found here but offers no information about what values are expected. Based on my research, I'm supposed to make an If statement but I don't know what I'm checking for.
Advice on how to solve the error is appreciated. Thank you.
You need to return a bool.
public bool OnDown(bool held)
{
var path = "pack://siteoforigin:,,,/Themes/FamiGami/Images/Controls/Atari 2600/Joystick.png";
Controller2.Source = new BitmapImage(new Uri(path));
return true;
}
I need to add some ResourceDictionary to a WPF window.
If I do as follow, everything works:
var uri = $#"..\..\..\Assets\Styles";
if (Directory.Exists(uri))
{
var allFile = Directory.GetFiles(uri);
if (allFile == null) return;
foreach (var file in allFile)
{
backupManagementWindow.MergeResourceDictionary(new Uri(file, UriKind.RelativeOrAbsolute));
}
}
Since that I don't want to define the Uri based on my executable directory, I define it as follow:
var uri = AppDomain.CurrentDomain.BaseDirectory + #"Assets\Styles";
The Uri is correct (there are the files) but then the code raise an exception.
public void MergeResourceDictionary(Uri uriResource)
{
var newResource = new ResourceDictionary {Source = uriResource};
Resources.MergedDictionaries.Add(newResource);
}
Does anyone know why? Thanks!
You can use Pack Uri Scheme when working with resource dictionaries.
And your uri would look something like this:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
I'm trying to print the contents of a WPF WebBrowser control so that no print dialog is shown, but am having no luck.
I have tried the following and am sure it did work:
PrintDialog printDialog = new PrintDialog();
printDialog.PrintDocument(((IDocumentPaginatorSource)browser.Document).DocumentPaginator, "My App");
but for some reason, I'm now getting the following exception:
Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to interface type 'System.Windows.Documents.IDocumentPaginatorSource'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{2C0C27DF-282F-3225-ADCD-CEC68F890EEB}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
The only thing I can think has changed on my PC is that I have installed IE8 since I last tried this, but would that really break it?
// Send the Print command to WebBrowser. For this code to work: add the Microsoft.mshtml .NET reference
mshtml.IHTMLDocument2 doc = WebBrowser1.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, null);
Source:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/63ae5345-b2ca-45a9-9113-0ddc43e9925b
For silent printing without dialogs, use this code:
private void PrintCurrentPage()
{
// document must be loaded for this to work
IOleServiceProvider sp = WebBrowser1.Document as IOleServiceProvider;
if (sp != null)
{
Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");
const int OLECMDID_PRINT = 6;
const int OLECMDEXECOPT_DONTPROMPTUSER = 2;
dynamic wb; // should be of IWebBrowser2 type
sp.QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, out wb);
if (wb != null)
{
// this will send to the default printer
wb.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, null, null);
}
}
}
[ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IOleServiceProvider
{
[PreserveSig]
int QueryService([MarshalAs(UnmanagedType.LPStruct)] Guid guidService, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
}
WebBrowser silent printing
To avoid taking a dependency on MSHTML you can simply do:
browser.InvokeScript("execScript", new object[] { "window.print();", "JavaScript" });
I devised the above solution when I was having trouble with a WPF project that was migrated to .Net Core 3 and was using Tony's answer that involves MSHTML reference. Referencing MSHTML in .Net Core is not very straightforward (see this github issue)
I've created a Silverlight project that produces [something].xap file to package a few silverlight UserControls. I would like to manipulate that .xap file through the use of javascript in the browser to show and hide user controls based upon java script events.
Is it possible to do this?
If so any sample could or links to documentation would be appreciated.
Thanks in advance
Kevin
Here's my solution...not sure if it's the "best-practices" way...comments????
In the App class within my Silverlight application I have the following code:
private Page _page = null;
private void Application_Startup(object sender, StartupEventArgs e)
{
_page = new Page();
this.RootVisual = _page;
HtmlPage.RegisterScriptableObject("App", this);
}
Also to the App class I add a [ScriptableMember] to be called from JavaScript
[ScriptableMember]
public void ShowTeamSearch(Guid ctxId, Guid teamId)
{
_page.ShowTeamSearcher(ctxId, teamId);
}
The Page class is the default one that get's created within the Silverlight Control project, it really doesn't have any UI or logic, it's just used to swap in/out the views.
Login oLogin;
TeamSearcher oSearcher;
public Page()
{
InitializeComponent();
oLogin = new Login();
oSearcher = new TeamSearcher();
oLogin.Visibility = Visibility;
this.LayoutRoot.Children.Add(oLogin);
}
Also a method is added to show/hide the views...this could/will probably get more advanced/robust with animations etc...but this shows the basic idea:
public void ShowTeamSearcher(Guid ctxId, Guid teamId)
{
oSearcher.UserTeamId = teamId;
oSearcher.UserContextId = ctxId;
LayoutRoot.Children.Remove(oLogin);
LayoutRoot.Children.Add(oSearcher);
}
Then to invoke this in the JavaScript after assigning the id of oXaml to the instance of the silverlight host.
var slControl = document.getElementById('oXaml');
slControl.Content.App.ShowTeamSearch(sessionId, teamId);
This seems to work and isn't all that bad of a solution, but there might be something better...thoughts?
Here is a my collections of my links for this subject.
Javascript communication to
Silverlight 2.0
Silverlight
interoperability
Silverlight
and JavaScript Interop Basics