How to get MVVM Light Messenger working with a PCL - c#

I am currently reorganizing my WP8 project in order to have a Portable Class Library, which could result in a W8 app.
I am using MVVMLight and have the following code:
In the Portable Class Library:
namespace MyApp.Shared.Messages
{
public class MyItemSelectedMessage : MessageBase
{
public MyItemSelectedMessage(MyItem item)
{
Item = item;
}
public MyItem Item { get; set; }
}
}
In my WP8 app, I still do have the ViewModels (because of Telerik and some other stuff which I cannot outsource to the PCL), where I use the following code to register for the message:
Messenger.Default.Register<MyItemSelectedMessage>(this, msg => SelectedItem = msg.Item);
The code worked fine before but now VS gives me the following error in the WP8 app at the given line:
The type 'GalaSoft.MvvmLight.Messaging.MessageBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'GalaSoft.MvvmLight, Version=4.2.30.16997, Culture=neutral, PublicKeyToken=null'
In my WP8 project, I have installed MvvmLight (v4.2.30.23483) and in my PCL there are the MvvmLight Libraries for PCL (v4.2.30.16997), both up-to-date from Nuget. Do I need to downgrade a package to get the same build number, add the libraries manually or what can I do in order to make it work?
Thanks in advance!

You should reference the Portable.MvvmLightLibs NuGet package from both projects, and not any of the non-portable MvvmLight packages. The Portable.MvvmLightLibs package includes support for both PCLs and platform-specific libraries.

Related

System.TypeLoadException occurred when i try to compile

System.TypeLoadException'Could not resolve type with token
01000016 from typeref (expected class
'Xamarin.Forms.Xaml.Diagnostics.VisualDiagnostics' in assembly
'Xamarin.Forms.Core, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=null')'
this is the App.cs File where this error appears :
namespace NewApp
{
public partial class App : Application
{
public App()
{
InitializeComponent(); // GETTING THE ERROR
Material.Init(this);
MainPage = new MaterialNavigationPage(new MainPage());
}
}
}
I had the same issue when I added the NUGET package Xamarin.Forms.
Check your Android Xamarin.Forms NuGet Package and the same for your IOS project.
You will notice your versions are not matching.
Right click update on each package to update to the latest version.
Do this for all including the Xamarin main project. Then delete the bin and obj folder for each project in your file system. Xamarin, IOS and Android.
Rebuild. Then launch.
Should work for you as well.
Same thing happened to me during an upgrade of Xamarin.Forms.
The issue seemed to have only been isolated to the iOS build.
To fix this issue, remove the Xamarin.Forms dependency from all the projects in the solution and reinstall Xamarin.Forms through nuget.
Then Clean All --> Rebuild All
I had same problem.
Just check if the Xamarin packages are the same version in /Packages, and /Dependencies/NuGet folders.
After that, Clean All and Rebuild All.

Autofac null service resolve exception

I have a 3 project in my solution:
Portable Library (for ios, android and winphones), library project(for windows phone) and windows phone test project. My library project got reference to portable library project, and my windows phone test project got reference to portable and winphone library project. In my portable library project I got some service interface which I implement in library winphone project. In my portable project I want have some class that have fields with service that I implement in windows phone library and use this class in windows phone test project. To do this i try use Autofac library, to register types in my windows phone library( to do this I made class with static ctor) and try to resolve service in my portable library class which I register in my windows phone library, but I got null exception and I don't know how to fix that, and it is possible to do things which I actually trying to do ?
My class from portable library project, after call Resolve method I go exception.
public IDeviceInformation DeviceInformation { get; }
public Logger()
{
DeviceInformation = AutofacContainer.Instance.Resolve<IDeviceInformation>();
}
Class from library project
public class AutoFacRegister
{
static AutoFacRegister()
{
var builder = new ContainerBuilder();
builder.RegisterType<WindowsDeviceInformation>()
.As<IDeviceInformation>();
AutofacContainer.Instance = builder.Build();
}
}
I'd recommend against using a static constructor to create your container. I suspect that the constructor is never called since the AutoFacRegister class is not used anywhere and therefore AutofacContainer.Instance will always be null.
I would suggest to use modules to achieve your goal.

Xamarin.Forms using SQLite.Net.Async (the NUGET part)

I'm trying to make a connection to a SQLite database using Visual Studio 2015 the target platform is Android.
I've found this SO question but I can not get it to work. My problem is the part of installing packagde.
I want to have my database stuff placed in a Class Library so I've created a new Application, and added a Class Library to the solution.
To install SQLite I've written the following in the packagde manager console:
Install-Package SQLite.Net.Platform.XamarinAndroid -Version 2.3.0
But installs SQLLite into my application, not my Class Library.
My Class Library is very very simple:
public class SQLiteDataService
{
private SQLiteAsyncConnection connectionFlexCheckDemo;
private ISQLitePlatform sqlitePlatform;
private string FlexCheckDBNameDemo = "FlexCheckDemo.db";
private void Test()
{
var platform = new SQLitePlatformAndroid();
}
}
but it doesn't compile:
Error CS0234 The type or namespace name 'Net' does not exist in the
namespace 'SQLite' (are you missing an assembly reference?) ...
What am I doing wrong? And more important how to make it work.
You will have to install the Nuget or the dll to reference the SQLite-Async in your Shared or PCL project so that you can write your common code there like having common CRUD operations or having Unit Of Work coupled with Repository pattern. A simple example can be found here.
Then in your platform specific projects you need to have the nuget installed as well, which will get resolved and install the corresponding version based on your platform. I don't think there is a nuget available for WP, so you might have to look for other choices.
This is the nuget package for SQLite.Net.Async PCL

Nuget Package from a Class Library Required in Main Project?

I have created a Data Access Layer as a Class Library. This class library has a nuget package installed for my ORM. When I try to use my Class Library in my main project it complains that the main project doesn't have my ORM Nuget Package installed.
I thought that if the Class Library had that Nuget Package installed then I wouldn't need that package installed for every project that uses it.
Am I doing something wrong here or is that just the way it works?
Thanks!
UPDATE
In my DAL I have a DataRepositoryBase class that has a method called GetPaged. Under the covers this DataRepositoryBase class uses ServiceStack's ORMLite.
In my Program I have the following code:
using (DataRepositoryBase<Inventory> InvRepo = new DataRepositoryBase<Inventory>())
{
IEnumerable<Inventory> invList = InvRepo.GetPaged(i => i.Cust_ID == CustID, 0, 10);
}
My main program is complaining about not having a reference to ORMLite.
If you are referencing types in the DAL from the Main Project then you have to reference that assembly from there as well.

C# Portable Class Library - How do you include System.Data.Linq

Is it possible to reference the System.Data.Linq in a Portable Class Library project?
Note:
I am just trying to share code between a WP8 and WinStore8 app [DataContext]
No it is not. The Data namespace is unavailable in PCLs.
You can tell because http://msdn.microsoft.com/en-us/library/system.data.aspx none of its members have the PCL icon, and it is not listed on http://msdn.microsoft.com/en-us/library/vstudio/gg597391%28v=vs.100%29.aspx

Categories

Resources