Xamarin FFImageLoading Usage Clarification - xamarin.android

I see we use FFImageLoading like below
var cachedImage = new CachedImage() {
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
WidthRequest = 300,
HeightRequest = 300,
...
Source = <url or asset or resource location>
};
or in XAML:
<ffimageloading:CachedImage
HorizontalOptions="Center" VerticalOptions="Center"
WidthRequest="300" HeightRequest="300"
DownsampleToViewSize="true"
Source = "<url or asset or resource location>>
</ffimageloading:CachedImage>
, so, I replaced all instances of Image in my UWP project and ImageView in my Android project with CachedImage.
But after reading through FFImageLoading documentation, I also see lots of
cases where images are loaded using ImageService. For example:
ImageService.Instance.LoadUrl(urlToImage).Into(_imageView);
ImageService.Instance.LoadCompiledResource(nameOfResource).Into(_imageView);
...
What is the difference between these two ways?
Why would I use one over the other?

FFImageLoading is a multi-platform library. ImageService.Instance methods are used to load images into native views (like ImageViewAsync on Android or UIImageView on iOS) and also for some advanced scenarios. There are also platform specific controls which internally use those methods, like:
CachedImage for Xamarin.Forms
MvxCachedImageView for native Android/iOS/Windows or MVVM Cross
They allow you for using things like bindings out of the box.
I advice you to use platform specific controls and use ImageService.Instance calls for advanced things. But it's entirely up to you.

Related

Xamarin Android: F# icon not being picked up by Resources

Create an F# Xamarin Android project in Visual Studio 10.
The template contains a set of mipmap folders, to which you can add your own icons.
If I add the icon my_icon.png to each of the mipmap folders, then they should be picked up as a drawable resource: i.e. I should be able to use
type MyResources = MyProject.Resource
and then the compiler should pick up
let myIcon = MyResources.Drawable.my_icon
But it isn't.
Is this a bug? And is there a workaround?
The answer is laughably obvious.
The resource lives in MyResources.Mipmap:
let myIcon = MyResources.Mipmap.my_icon

Image from custom.xcassets in React-Native

If we want to use an image in React-Native js from Images.xcassets, we simply provide the image name as URI - for eg.
<Image style = {styles.someStyle} source = {{uri:'addImage.png'}}>
But what if we have a custom created .xcassets ? How can that imageset be accessed from the RN js ?
It's considered as legacy code, but can be imported like:
<Image source={require('image!custom')} />

Xamarin WebView local page - why separate resources per platform

I need to display a local page in xamarin forms. I followed this documentation: Xamarin working with WebView and I use following code to display a page:
private void DisplayLocalPage(string filename)
{
HtmlWebViewSource htmlSource = new HtmlWebViewSource();
var html = new HtmlWebViewSource();
html.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
html.Html = ReadFile(filename);
MyWebView.Source = html;
}
The page uses css in separate files - according to documentation, I have to use dependency service to make possible for each platform to reach css files.
So my project looks like this:
TestWebView (Portable):
Resources:
local_page.html
stylesheet.css
TestWebView:Droid
Assets:
stylesheet.css
TestWebView:iOS
Resources:
stylesheet.css
As long as I can have html only in portable project, why do I have to use platform-specific resources for css? And is there any way to store the css also only once? The documentation didn't mention that.

Printing from a Xamarin.Forms app

I'm all new to Xamarin and I'm currently working on a sample or a "prove of concept" app using Xamarin.Forms.
I'm supposed to perform a print task from this app though I'm not at this point sure what to print yet (the screen, content of a label, a file etc.).
Either way, what is the easiest way to print from a Xamarin.Forms app?
(current target is primarily Android 4.4+).
I hope this isn't too complicated :)
EDIT:
Ok let me just update this post as the original text might be a bit ambitious/vague.
I have a Xamarin.Forms project (+ an Android part) and I have some HTML available in the XF part of the project that I need to get into a WebView and print it.
From what I understand, the thing with the WebView has to be done on the Android part of the project due to the fact that this is where the printing will be handled.
I was hoping this could be done from code since I don't really need to display the WebView, just print it's content.
The Android part of the project has only the MainActivity and no layouts or XAML files.
I don't know where to add the WebView or how to access it (other than DependecyService seems to be a buzz word here) so I'm kinda stuck here.
I'm thinking that this task should be rather trivial to someone with a little more Xamarin experience than me.
Every platform XF supports has it's own mechanism for printing. XF does not provide any abstractions for printing in a cross-platform manner. You will need to write printing logic for each layer and expose it to XF using DependencyService (or some other DI engine).
Here is a good example, of course, using dependency service:
https://codemilltech.com/xamarin-forms-e-z-print/
I so wanted to do this but it was too hard. Finally built it into Forms9Patch - a MIT licensed open source project.
Verifying that Printing is available
Before printing, you should verify that printing is available on your device. To do so, call:
if (Forms9Patch.PrintService.CanPrint)
{
// do the printing here
}
Print the contents of a Xamarin.Forms.WebView
using Forms9Patch;
...
var myWebView = new Xamarin.Forms.WebView
myWebView.Source = new HtmlWebViewSource
{
Html = "some HTML text here"
};
...
myWebView.Print("my_print_job_name");
Note that your WebView does not have to be attached to a Layout. This allows you to Print without having to display the WebView in your app’s UI.
Printing an HTML string
using Forms9Patch;
...
var myHtmlString = #"
<!DOCTYPE html>
<html>
<body>
<h1>Convert to PNG</h1>
<p>This html will be converted to a PNG, PDF, or print.</p>
</body>
</html>
";
...
myHtmlString.Print("my_print_job_name");
PLEASE NOTE: iOS sometimes places the page breaks in weird places. I have a StackOverflow Bounty on why this happens and how to fix it.
Using EmbeddedResource as a source for a Xamarin.Forms.WebView
This is sort of an experimental feature I’ve built that I’ve found it useful. As such the documentation is sparse. It allow you to put HTML content in a folder in your app’s EmbeddedResources folder and then use it as a source for a WebView. A much nicer solution than using platform specific approach provided by Xamarin. It also supports putting all of the HTML content into a zip file. Please take a look at the source code to see how it works.
You can handle the printing of lists/ invoices .. with the xfinium pdf component from xamarin componentstore. With that you create your _pdffile and then call the following method which starts the adobereader from where you can select a printer (in my case google cloudprint)
public void printPdfToCloud(string _pdffile)
{
try
{
var saveto = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "YourApp/"+_pdffile);
string file_path = saveto;
if (System.IO.File.Exists(file_path))
{
Android.Net.Uri pdfFile = Android.Net.Uri.FromFile(new Java.IO.File(file_path));
Intent pdfIntent = new Intent(Intent.ActionView);
pdfIntent.SetPackage("com.adobe.reader");
pdfIntent.SetDataAndType(pdfFile, "application/pdf");
pdfIntent.SetFlags(ActivityFlags.NoHistory);
StartActivity(pdfIntent);
}else
{
// give a note that the file does not exist
}
}
catch (Exception E)
{
// Do some Error dialog
}
}

iOS 8 + Swift - Looking to define multiple lanes, but draw them based on events

I have 3 buttons in one of my app storyboards. Based on the button pressed by the user, I would like to have a different line drawn. I'm new to iOS development and I was looking at Apple's Developer Library for details on how to create a context, path and line definition, but I was not able to figure out the implementation to define the contexts and paths for multiple lines.
Thanks in advance for the help.
Edit - 2015-02-24: To clarify the question and adding a video explaining how I do this on another platform.
Because the question might be difficult to understand, and because I have more experience with Windows development, I created a Windows Phone app showing what I'd like to do on iOS+Swift.
Here is how I do this in XAML + C# for Windows Universal Apps (Runtime)
First, I define the line in MainPage.xaml (The Tranform tags are added later):
<Line x:Name="line1" Stroke="Black" StrokeThickness="6" Y1="60" Y2="60" RenderTransformOrigin="0.5,0.5" Margin="1,0,-1,0" X1="30" X2="30">
<Line.RenderTransform>
<CompositeTransform/>
</Line.RenderTransform>
</Line>
Then I define the storyboard animation within the Page.Resources section in the XAML file:
<Storyboard x:Name="anLine2">
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="line1" d:IsOptimized="True"/>
<DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(Line.X2)" Storyboard.TargetName="line2">
<EasingDoubleKeyFrame KeyTime="0" Value="30"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.30" Value="380"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
I define the button:
<Button x:Name="btnL1" Content="Line 1" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="10,0,0,0" Click="btnL1_Click"/>
And finally I trigger the animation <> when the button is pressed:
private void btnL1_Click(object sender, RoutedEventArgs e)
{
anLine1.Begin();
}
I defined 3 lines and 3 buttons. The result is seen in this video (It looks a bit choppy in the video, but it is because of the screen capture software. The execution in the emulator and test devices is very smooth):
http://youtu.be/Y4i2IUehl7U
Now, what I want is being able to do exactly this, but on iOS using Swift. I hope this helps clarifying the question.
Thanks in advance!
I think what you want is to animate a line being drawn - if so, see this questions:
how to draw line with animation?
In particular one of the answers has this link:
http://tumbljack.com/post/179975074/complex-interpolation-with-cashapelayer-free
Basically, use a CAShapeLayer and animate from an empty path to a path with your destination.

Resources