I want create a very simple HTML parser application. I read lots of tutorial and lots of developer use this class: HtmlDocument.
I want use this class in my app too but I am not able to add reference to System.Windows.Forms.
I try to add reference in Project > Reference but I can't find Windows.Forms.
How can I fix this problem and use HtmlDocument?
I use Visual Studio 2013.
Thank you.
This is my very simple code:
namespace ParseHTML
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
string url = "http://www.alvolante.it/";
download(url);
}
private async void download(string url)
{
HttpClient client = new HttpClient();
string risposta = await client.GetStringAsync(new Uri(url)); //download html della pagina web
HtmlDocument hc = new HtmlDocument(); //error here, missing reference or assembly?
}
You can't. WinForms is not supported nor even implemented on Windows Phone (Windows Mobile 6.5.3 released in early 2010 is the last "Windows" phone OS to support WinForms by way of the Compact Framework).
To process HTML in applications, I suggest HtmlAgilityPack instead, which provides a fault-tolerant DOM manipulation library: http://htmlagilitypack.codeplex.com
Related
I have created a small library in c# and want to display a help url like
class HelpURL : Attribute
{
string url;
public HelpURL(string url)
{
this.url = url;
}
}
[HelpURL("www.example.com")]
public func() {
...
}
It works inside my library project but i dont know how to display it in another project where my library is added as a reference? How can i do that?
Edit I tried using XML comments but dont see any changes.
I recommend you use Visual Studio's built-in intellisense features instead. All you need to do is add XML documentation comments to your code, and it will automatically be visible in your other projects.
I am implementing Share the App feature in Xamarin forms project. Yes, there are libraries which are available, But library conflicts with a version of Xamarin forms package and I have already completed all the stuff and don't want any issue, and I want to implement using dependency service by doing platform specific coding in Android and iOS without any package.
Here, I am using intent for share the app from Android project by making custom renderer. But I am getting error.
public class ShareTheAppRenderer : IShareTheApp
{
public void ShareApp()
{
var mainActivity = new MainActivity();
Intent sendIntent = new Intent();
sendIntent.SetAction(Intent.ActionSend);
sendIntent.PutExtra(Intent.ExtraText, "Check out our app at: https://play.google.com/store/apps/details?id=");
sendIntent.SetType("text/plain");
mainActivity.StartActivity(sendIntent);
}
}
Please give some suggestions to resolve this issue.
Thank you for your reply. But I got the solution for this. Here I don't want to use any other libraries to achieve this.
My Working solution is as below:
public class ShareTheAppRenderer : IShareTheApp
{
public void ShareApp()
{
var appPackageName = Forms.Context.PackageName;
var myIntent = new Intent(Android.Content.Intent.ActionSend);
myIntent.SetType("text/plain");
myIntent.PutExtra(Intent.ExtraText, "Check out our app at: https://play.google.com/store/apps/details?id=" + appPackageName);
Forms.Context.StartActivity(Intent.CreateChooser(myIntent, "Choose an App"));
}
}
This solution works for me.
I am working on an app where I want users to get images from web service but don't want them to be stored locally in localdb or local files.
Is there a way to do it?
I am using Xamarin forms.But if it can be done natively it can also be done through xamarin forms.
You can always download the image as a file using a webclient and then an StreamImageSource.
First, you need an interface for a dependency service (I prefer to use WebClient than any third party library so I use these services, feel free to use anything else if you like it, just skip the service part) created on the Forms project:
public interface IDownloader
{
byte[] Download(string Url);
}
Then, you need a dependency service on the Android project:
[assembly: Dependency ( typeof (--yournamespace--.Downloader))]
public class Downloader : IDownloader
{
public byte[] Download(string Url)
{
//This code is synchronous, I would recommend to do it asynchronously
WebClient wc = new WebClient();
return wc.DownloadData(Url);
}
}
And then you can download the image and set it as source:
//I assume there exist an image control called img.
var dl = DependencyService.Get<IDownloader> ();
byte[] data = dl.Download(--url to the image--);
var ms = new MemoryStream(data);
img.Source = new StreamImageSource{ Source = (t) => ms };
You don't mention if you're online or not - if you're online you can just use a URL in the XAML for Xamarin forms, eg:
<Image HeightRequest="50" WidthRequest="50" HorizontalOptions="Center" VerticalOptions="Center" Source="{Binding UserAvatarURL}"/>
And Forms will handle it.
hope its something simple stupid, but I am spinning my wheels.
I have a Surface Pro 4, Windows 10 and using Visual Studio 2013 Professional.
Developing WPF using C# 4.5.
In summary, all I am trying to do a simple camera capture to save an image without resorting to other 3rd party libraries I have no control over. The rest of this post are details of other research findings that I HAVE looked into and tried working out and what has failed and what such message from the compiler.
Again, simple camera, capture picture, save to disk, but all the async-await options appear to throw compiler errors.
EDIT 1 SAMPLE CODE
Here is code from the WPF form. I do not even have any control in the form as I can not even get the 'await' to compile.
using System;
using Windows.Media.Capture;
using System.Windows;
namespace CameraCapture
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
JustDoIt();
}
private MediaCapture _mediaManager;
private async void JustDoIt()
{
//initialize mediacapture with default camera
_mediaManager = new MediaCapture();
await _mediaManager.InitializeAsync();
if (_mediaManager == null)
MessageBox.Show("Failed Initialize");
await _mediaManager.StartPreviewAsync();
}
}
}
And my project also has per other links researched from below, the dlls for
System.Runtime.dll and
System.Runtime.WindowsRuntime.dll
Compile error for each of the 'await'
Error 1 'await' requires that the type 'Windows.Foundation.IAsyncAction' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?
and I have "using System;" as the first line. Is there some other "await" going on when using WinRT vs default System include?
END OF EDIT
I started with https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.capture.cameracaptureui.aspx
CameraCaptureUI class
I then found this link https://www.eternalcoding.com/?p=183
How to use specific WinRT API from Desktop apps
I tried going through all the steps as outlined and getting errors associated with
await ... have a suitable GetAwaiter method.
So, I looked up about asynch and await and came across
How and When to use `async` and `await`
how and when to use async and await.
So, I scrapped the first camera capture project, started a new, and did that version
that has simple thread.sleep delay to show the concept of asynch / await. That part works.
So now, I add back the reference to the project and manually edit to add the
<PropertyGroup>
<TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>
to expose the References expose the Windows / Core per the first link and then getting access to the Windows.Media, Windows.Storage. I add in just the first little bit of code about the CameraCaptureUI and CaptureFileAsync as just a starting point such as...
private async void Camera1()
{
var cameraUi = new Windows.Media.Capture.CameraCaptureUI();
var capturedMedia = await cameraUi.CaptureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.Video);
if (capturedMedia == null)
return;
MessageBox.Show("Valid Camera");
}
and get a compile error about:
'await' requires that the type
'Windows.Foundation.IAsyncOperation'
have a suitable GetAwaiter method.
Are you missing a using directive for 'System'?
Also, back to the original Windows version attempt via
private async void Camera2()
{
CameraCaptureUI dialog = new CameraCaptureUI();
Windows.Foundation.Size aspectRatio = new Windows.Foundation.Size(16, 9);
dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;
StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file == null)
return;
MessageBox.Show("Valid Storage File Captured");
}
I even have System.Runtime and System.RunTime.WindowsRuntime as references to the project but still fail on compile.
What am I missing. I know things change between different versions such as Windows 8.1 and Windows 10, and upgrades to features / libraries, but why await works one way, but not another.
For those scratching their heads as I did, I finally came across another post that had a missing link...
How can i import windows.media.capture in my WPF project?
The difference was the
<TargetPlatformVersion>8.1</TargetPlatformVersion>
instead of 8.0.
8.1 showed many of the individual .dll references of Foundation, Media, etc but not the single "Windows" dll.
Once changed to 8.1 and recognized the WINDOWS dll, all worked with respect to the media manager or CameraCaptureUI options.
I have searched nearly everywhere, but cannot find a way of creating/inserting a new Page/Tab in C# within a Visio document. I recorded a VB Macro of creating a new page within a document, and it is really simple there. However, I am using C# and cannot find the right commnands. Thanks in advance!
Writing in C# you will use the same COM API which VBA uses. A simple way to automate Visio using C# is to download and install the Primary Interop Assembly (PIA). Then include the reference Microsoft.Office.Interop.Visio in your project. Here is a simple example of using the PIA to manipulate the pages in a Visio document.
namespace VisioExample
{
using System;
using Microsoft.Office.Interop.Visio;
class Program
{
public static void Main(string[] args)
{
// Start Visio
Application app = new Application();
// Create a new document.
Document doc = app.Documents.Add("");
// The new document will have one page,
// get the a reference to it.
Page page1 = doc.Pages[1];
// Add a second page.
Page page2 = doc.Pages.Add();
// Name the pages. This is what is shown in the page tabs.
page1.Name = "Abc";
page2.Name = "Def";
// Move the second page to the first position in the list of pages.
page2.Index = 1;
}
}
}
To learn about developing solutions you can look at the Developing Visio Solutions book online. Download the Visio SDK, it contains a library of sample code in C#. You could look at "Visio 2003 Developer's Survival Pack" by Graham Wideman. As you found, the macro recorder can show you the API methods you need to call to achieve a task. The COM API used by VBA are the same API you will use in C#, the syntax of the code will differ obviously.