|
Actually,it has been awhile since I wrote that, but I was able to find it. I am not sure I included all the relevant classes, but below you will find three I know are involved. Since you are not able to inject EventAggregator in the
constructor, I used the ServiceLocator to get it. I think another alternative could be to use property injection with a buildup method. ServiceLocator works great though. The idea is to publish event using eventagg and then subscribe
to that event in one of your view models. In my case I think I had some progress spinner that was bound to an int Progress property in my ApplicationViewModel where the view is Shell.xaml. I registered for the event in ApplicationViewModel
and set the progress property from the event handler. I also think I made a IntegerToVisibility Converter that i used for the binding between the progress control and the progress property. Basically return (value >0 &&
<100) ? Visibility.Visible : Visibility.Collapsed.
If I remember right, I was uncertain about overriding ModuleTypeLoaders property because I would have preferred to just add a new IModuleTypeLoader to it since it is an IEnumerable, rather than overriding the property. For some reason I decided to
just override the property which works, but there is likely a better way to accomplish this that I missed. .....Hope this helps!
public class VerbFileDownloader : IFileDownloader
{
private readonly WebClient webClient = new WebClient();
private readonly IEventAggregator eventAgg;
public VerbFileDownloader()
{
eventAgg = ServiceLocator.Current.GetInstance<IEventAggregator>();
}
private event EventHandler<DownloadCompletedEventArgs> _downloadCompleted;
public event EventHandler<DownloadCompletedEventArgs> DownloadCompleted
{
add
{
if (this._downloadCompleted == null)
{
this.webClient.OpenReadCompleted += this.WebClient_OpenReadCompleted;
this.webClient.DownloadProgressChanged += this.DownloadProgressChanged;
}
this._downloadCompleted += value;
}
remove
{
this._downloadCompleted -= value;
if (this._downloadCompleted == null)
{
this.webClient.OpenReadCompleted -= this.WebClient_OpenReadCompleted;
this.webClient.DownloadProgressChanged -= this.DownloadProgressChanged;
}
}
}
void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
RaiseDownloadProgressChanged(e.ProgressPercentage, false);
}
private void RaiseDownloadProgressChanged(int val, bool isComplete)
{
eventAgg.GetEvent<ModuleDownloadProgressEvent>().Publish(new ModuleDownloadProgressArgs(val, isComplete));
}
public void DownloadAsync(Uri uri, object userToken)
{
this.webClient.OpenReadAsync(uri, userToken);
}
private void WebClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
this._downloadCompleted(this, ConvertArgs(e));
RaiseDownloadProgressChanged(0, true);
}
private static DownloadCompletedEventArgs ConvertArgs(OpenReadCompletedEventArgs args)
{
return new DownloadCompletedEventArgs(args.Error == null ? args.Result : null, args.Error, args.Cancelled, args.UserState);
}
}
public class VerbModuleManager : ModuleManager
{
public VerbModuleManager(IModuleInitializer moduleInitializer, IModuleCatalog moduleCatalog, ILoggerFacade loggerFacade)
: base(moduleInitializer, moduleCatalog, loggerFacade) { }
private System.Collections.Generic.IEnumerable<IModuleTypeLoader> typeLoaders;
public override System.Collections.Generic.IEnumerable<IModuleTypeLoader> ModuleTypeLoaders
{
get
{
if (this.typeLoaders == null)
{
this.typeLoaders = new List<IModuleTypeLoader>()
{
new VerbXapModuleTypeLoader()
};
}
return this.typeLoaders;
}
set
{
this.typeLoaders = value;
}
}
}
public class VerbXapModuleTypeLoader : XapModuleTypeLoader
{
protected override IFileDownloader CreateDownloader()
{
return new VerbFileDownloader();
}
}
|