ZXing.Net generated Barcode doesn't appear in Xamarin Forms - c#

I add ZXing.Net.Mobile, ZXing.Net.Mobile.Forms to project.
Here is the code:
using ZXing.Net.Mobile.Forms;
using ZXing.Net.Mobile;
namespace discountapp
{
public partial class discountappPage : ContentPage
{
ZXingBarcodeImageView barcode;
public void Handle_Clicked(object sender, System.EventArgs e)
{
barcode = new ZXingBarcodeImageView
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
barcode.BarcodeFormat = ZXing.BarcodeFormat.EAN_13;
barcode.BarcodeOptions.Height = 25;
barcode.BarcodeOptions.Width = 75;
barcode.BarcodeValue = "2800100028014";
Content = barcode;
}
}
}
For iOS platform also add ZXing.Net.Mobile, ZXing.Net.Mobile.Forms and put some changes in AppDelegate's FinishedLaunching() implementation:
global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();
For Android platform also add ZXing.Net.Mobile, ZXing.Net.Mobile.Forms and put some changes in MainActivity's OnCreate() method:
global::ZXing.Net.Mobile.Forms.Android.Platform.Init();
But after I click the button, it doesn't show Barcode, just nothing without error. Could you help, please!

This issue on the ZXing repo suggests adding
WidthRequest = 410,
HeightRequest = 410,
to ZXingBarcodeImageView which makes the barcode appear although with a bad aspect ratio.

Related

Image from ImageSource

I tried to follow the following example by #CraigDunn with the sample code
someButton.Image = ImageSource.FromFile("imageName.png");
My own try looks like this:
_NavBarPrevPage = new Button
{
Image = ImageSource.FromResource("media_step_back.png")),
BackgroundColor = Color.Red,
TextColor = Color.White,
};
For me, the IDE tells me:
The type Xamarin.Forms.ImageSource can't be converted to Xamarin.Forms.FileImageSource. An explicit conversion exists. Perhaps a conversion is missing.
I think I'm doing exactely what #CraigDunn proposed.
What am I doing wrong?
I've tried the following, and I was able to get rid of the conversion errors in the current answer, but the image is not shown on the button, while it is shown in an image, so the file name is correct:
var nImg = new FileImageSource()
{
File = "media_step_back.png",
};
_NavBarPrevPage = new Button
{
Image =nImg,
};
If the thing you need is a button with image that is implementing by code-behind :
Button button = new Button
{
BackgroundColor = Color.Red,
Font = Font.Default,
FontSize = 10,
TextColor = Color.White,
HeightRequest = 35,
WidthRequest = 80
};
button.Image = "media_step_back.png";
this.Content = button;
Please remember to put the image file (media_step_back.png) in your target platform's resource folder
UPDATE:
You problem were you didn't add the image source in the Property folder and target platform's resource folder (Please note that in below example, I have added a xamarin.png image in each Xamarin "Property" folder, Android's "Resource" and iOS's "Resource" folder)
Below is the updated code that I placed the button into a StackLayout (which also able to works if without StackLayout) :
public class MainPageNew : ContentPage
{
public MainPageNew()
{
StackLayout parent = new StackLayout();
Button button = new Button
{
BackgroundColor = Color.Red,
Font = Font.Default,
FontSize = 10,
TextColor = Color.White,
HeightRequest = 300,
WidthRequest = 80
};
//button.Image = "media_step_back.png";
button.Image = "xamarin.png";
parent.Children.Add(button);
this.Content = parent;
}
Android
iOS:
This is the download link of the updated code.

Xamarin C# content page not loading image for Android JVM

I have a problem attempting to load an image for the Android JVM in use on Visual Studio 2017 when debugging. Below is a sample of my code:
public class ImageDisplayPage : ContentPage
{
public ImageDisplayPage()
{
Content = new ScrollView
{
Content = new StackLayout()
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{
new Image
{
IsVisible = true,
Aspect = Aspect.AspectFit,
Source = ImageSource.FromResource("Resources.a.png")
}
}
}
};
}
}
I have a folder in my project called "Resources" where the image is supposed to be loaded from, and have followed the path convention used in numerous posts on StackOverFlow and the guide listed here: https://developer.xamarin.com/guides/xamarin-forms/user-interface/images
I use Visual Studio 2017, coding in C# and when I attempt to debug this, the following message is what I get from the debug output:
Image data was invalid: Xamarin.Forms.StreamImageSource
The image in question is an 80kb PNG image.
Thank you in advance for any assistance
You should place your image inside the Resources/drawable folder of your android project and only place the image name and the extension.
public class ImageDisplayPage : ContentPage
{
public ImageDisplayPage()
{
Content = new ScrollView
{
Content = new StackLayout()
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{
new Image
{
IsVisible = true,
Aspect = Aspect.AspectFit,
Source = "a.png"
}
}
}
};
}
}

Opening platform specific screens after login screen in cross platform shared code Xamarin?

I want to open an Android activity for Android devices and iOS screen for iOS devices from login screen. The code of the login screen, I have written is in shared portable code Area. I have used interface and #if ANDROID #endif, but it is also not working.
Android screen will only contain a textview and an iOS screen will contain a Image. This is my Login Page in portable folder
namespace DemoApp
{
public class HomePage : ContentPage
{
public HomePage()
{
var title = new Label
{
Text = "Welcome to CloudCakes",
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
var email = new Entry
{
Placeholder = "E-Mail",
};
var password = new Entry
{
Placeholder = "Password",
IsPassword = true
};
var login = new Button
{
Text = "Login"
};
// With the `PushModalAsync` method we navigate the user
// the the orders page and do not give them an option to
// navigate back to the Homepage by clicking the back button
login.Clicked += (sender, e) =>
{
App.UserPreferences.Open();
// await Navigation.PushModalAsync(new MainPage());
};
Content = new StackLayout
{
Padding = 30,
Spacing = 10,
Children = { title, email, password, login}
};
}
}
}
And, this is the page in Droid folder where I want to navigate on clicking login button
name space
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : IUserPreferences
{
public Page1()
{
InitializeComponent();
}
public void Open()
{
var title = new Label
{
Text = "Welcome to Xamarin Forms IOS Screen!!",
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
Content = new StackLayout
{
Padding = 30,
Spacing = 10,
Children = { title }
};
}
}
}
Open is a method of interface which is present in the portable folder.
I assume that you're using Xamarin.Forms, so what you need to do is actually quite easy to achieve.
Instead of having a separate page for each platform, create one new XAML page with the following code:
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourApp.Views.MainPage">
<ContentView>
<OnPlatform x:TypeArguments="View">
<OnPlatform.iOS>
<Image Source="something.png" />
</OnPlatform.iOS>
<OnPlatform.Android>
<Label Text="Android" />
</OnPlatform.Android>
</OnPlatform>
</ContentView>
</ContentPage>
This way you can control what to have on the page for each platform.
To navigate to MainPage from your LoginPage, you need to implement the Clicked event (or preferrably use Commands) in the code behind (MainPage.xaml.cs) and do the navigation there:
private void SomeButton_Clicked(object sender, System.EventArgs e)
{
await this.Navigation.PushAsync(new MainPage());
}
In the long run, you should look into doing all this outside the code behind with ViewModel's and Commands.
Edit: Since the first solution didn't work for you, here's what to do:
1) Subclass ContentPage in your core project.
public class MainPage : ContentPage { }
2) Implement a PageRenderer for each platform separately. You'll be able to use native layout definition for your pages as you mentioned.
There's a great article on the Xamarin blog on how to achieve this. You can also find the source code on GitHub.

SimpleWPFReporting's PDF has no content

I'm trying to generate a very simple PDF just for practice by using SimpleWPFReporting.
Since there is no sample code in its documentation, I tried creating a sample from what I understood:
Window1.xaml.cs
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public class Sample
{
private string _aw;
public string Aw
{
get { return _aw; }
set { _aw = value; }
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
StackPanel stkMain = new StackPanel();
stkMain.Background = Brushes.Pink;
stkMain.Height = 100;
stkMain.Width = 100;
TextBlock txtSample = new TextBlock();
txtSample.Text = "SAMPLE TEXT";
stkMain.Children.Add(txtSample);
ReportOrientation portrait = ReportOrientation.Portrait;
Sample sample = new Sample()
{
Aw = "Some text"
};
Report.ExportReportAsPdf(stkMain, sample, portrait);
}
}
It does save as a PDF, but when I open it, it's blank. What I'm expecting is to see at least the TextBlock there.
I don't really understand the use of data context in the arguments, so I just added a class there. I tried looking for tutorials but failed. Did I miss something or did I create it wrong?
I believe you have to specify the stackpanel orientation as "vertical", as stated in the doc
https://github.com/maximcus/SimpleWPFReporting
After digging in to the source code, it uses a Visual,so you have to add the stackpanel to the visual tree in order to be renderer and then printed.
You can create a previewer window, add the stackpanel and now the pdf will show the content.
I will create a previewer by myself, so I will post when I finish it.

How to Remove a Xamarin.Forms TableView and Re-Create It Without Application Crashing

I have a StackLayout in Xamarin.Forms 1.4.2, and from code-behind I am trying to create a table view, and then just for a test I have a button to re-create the TableView in the StackLayout.
If I use the following, that uses a standard TextCell, then I can re-create the TableView as many times as I wish without the application crashing:-
This is on Windows Phone btw:-
TableView tableView1 = new TableView()
{
Intent = TableIntent.Form,
VerticalOptions = LayoutOptions.FillAndExpand
};
myStackLayoutControl.Children.Add(tableView1);
tableView1 = new TableRoot()
{
new TableSection("Section 1")
{
new TextCell()
{
Text = "Text1",
Detail = "Text2"
}
}
};
However when I change the TextCell to a ViewCell such as:-
new ViewCell()
{
View = new Label()
{
Text="hello",
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
},
Height = 100
}
It will display the TableView correctly on the first execution.
However clicking a button that clears down the TableView and executes the same code to re-create it will run the same code, however straight after it has finished the function it will crash the entire application with an Application_UnhandledException being caught!
The exception caught is:-
MeasureOverride of element 'Xamarin.Forms.Platform.WinPhone.ViewToRendererConverter+WrapperControl' should not return PositiveInfinity or Nan as its DesiredSize.
Does anyone have any ideas how to resolve this?

Categories