Change the directory of DataContext database file in WP8 - windows-phone-8

I am trying to change the location of the database file in my Windows Phone 8 app.
The default constructor places it in the root isolated storage directory: /MyDbFile.sdf/
public MyDataContext() : base("Data Source=isostore:/MyDbFile.sdf") { }
My goal is to put it in /db/MyDbFile.sdf
What I have tried (yes, I know that some of them are silly):
public MyDataContext() : base("Data Source=isostore:/db/MyDbFile.sdf") { }
public MyDataContext() : base("Data Source=isostore:/db\\MyDbFile.sdf") { }
public MyDataContext() : base("Data Source=isostore:\\db/MyDbFile.sdf") { }
public MyDataContext() : base("Data Source=isostore:\\/db/MyDbFile.sdf") { }
public MyDataContext() : base("Data Source=isostore:/db//MyDbFile.sdf") { }
public MyDataContext() : base("Data Source=isostore:/db\\/MyDbFile.sdf") { }
What is the proper way to do this? Is it even possible?

Figured it right after writing this question. You have to create the target directory first.
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!iso.DirectoryExists("db"))
{
iso.CreateDirectory("db");
}
}
_MyDataContext = new MyDataContext();
After that it works with:
public MyDataContext() : base("Data Source=isostore:/db/MyDbFile.sdf") { }

Related

mvvmcross - How to implement Single view for multiple ViewModels Or Single ViewModel for multiple Views

How to implement Single view for multiple ViewModels
Or Single ViewModel for multiple Views in MvvmCross
Thanks
public partial class App : MvxApplication
{
protected override void RegisterSetup()
{
//this.RegisterSetupType<MvxWpfSetup<Core.App>>();
// 注册自定义设置
this.RegisterSetupType<Setup>();
}
}
public class Setup : MvxWpfSetup
{
protected override IMvxApplication CreateApp()
{
return new Core.App();
}
protected override void InitializeViewLookup()
{
base.InitializeViewLookup();
// 自定义 视图& VM之间的关系
var container = Mvx.IoCProvider.Resolve<IMvxViewsContainer>();
var viewModelViewLookup = new Dictionary<Type, Type>()
{
{ typeof(NativeViewModel), typeof(Test) },
{ typeof(NestedChildViewModel), typeof(Test) }
};
//container.Add(typeof(NativeViewModel), typeof(Test));
//container.Add(typeof(NestedChildViewModel), typeof(Test));
container.AddAll(viewModelViewLookup);
}
}

Wildcat in AddRazorPagesOptions Conventions.AddPageRoute

I have this code:
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddRazorPagesOptions(options => {
options.Conventions.AddPageRoute("/Index", "Index.html");
options.Conventions.AddPageRoute("/rt", "rt.html");
});
}
Is it possible instead of writing each page, have one wildcat route like this?
options.Conventions.AddPageRoute("/*", "/{*.html}");
There is no built-in way to add such wildcard route. However you could achieve it with simple page route convention (implementation of IPageRouteModelConvention). Here is a working sample:
public class HtmlExtensionPageRouteModelConvention : IPageRouteModelConvention
{
public void Apply(PageRouteModel model)
{
var selectorCount = model.Selectors.Count;
for (var i = 0; i < selectorCount; ++i)
{
var attributeRouteModel = model.Selectors[i].AttributeRouteModel;
if (String.IsNullOrEmpty(attributeRouteModel.Template))
{
continue;
}
attributeRouteModel.SuppressLinkGeneration = true;
model.Selectors.Add(new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel
{
Template = $"{attributeRouteModel.Template}.html",
}
});
}
}
}
Configuration:
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddRazorPagesOptions(options => {
options.Conventions.Add(new HtmlExtensionPageRouteModelConvention());
});
}
Sample Project on GitHub

Topshelf TimeoutException

I'm trying to use Topshelf Framework to create a windows service. But when i try to start the service, there is this exception :
" The service failed to start... System.Service.Process.TimeoutException : the waiting period has expired and the operation has not been completed"
This is my code :
public class MyService : ServiceControl
{
private System.Timers.Timer _timer;
public void MyService()
{
_timer = new System.Timers.Timer(10);
_timer.AutoReset = false;
_timer.Elapsed += new ElapsedEventHandler(TimerOnElapsed);
}
private void TimerOnElapsed(object source, ElapsedEventArgs e)
{
//all the operation to do at the startup
}
public bool Start(HostControl hostControl)
{
_timer.Start();
return true;
}
public bool Stop(HostControl hostControl)
{
_timer.Stop();
return true;
}
}
Thanks for any help :)
There are several issues I notice:
The current code would make the timer fire only once (you have AutoReset = false)
with TopShelf, the MyService class should look like this:
using System.Timers;
using Topshelf;
namespace TopShelfTestService
{
public class MyService
{
private System.Timers.Timer _timer;
public MyService()
{
_timer = new System.Timers.Timer(10);
_timer.AutoReset = true;
_timer.Elapsed += new ElapsedEventHandler(TimerOnElapsed);
}
private void TimerOnElapsed(object source, ElapsedEventArgs e)
{
//all the operation to do at the startup
}
public bool Start(HostControl hostControl)
{
_timer.Start();
return true;
}
public bool Stop(HostControl hostControl)
{
_timer.Stop();
return true;
}
}
}
and the console app/ Program.cs will look like so:
using Topshelf;
namespace TopShelfTestService
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.Service<MyService>(s =>
{
s.ConstructUsing(name => new MyService());
s.WhenStarted((tc, hostControl) => tc.Start(hostControl));
s.WhenStopped((tc, hostControl) => tc.Stop(hostControl));
});
x.RunAsLocalSystem();
x.SetDescription("Sample Topshelf Host"); //7
x.SetDisplayName("Test Service with TopShelf"); //8
x.SetServiceName("TopShelfTestService");
});
}
}
}

AS3 "Type was not a compile time constant"

Ok, so here's my problem. I'm decompiling an old swf trying to get it to work. I've got it almost working, but I keep having the same problem. I keep getting the following error:
com\clubpenguin\tools\localtext\core\LocalTextProxy.as, Line 84 1046: Type was not found or was not a compile-time constant: Font.
Here's the code:
package com.clubpenguin.tools.localtext.core
{
import flash.text.Font;
public class LocalTextProxy extends Object
{
public function LocalTextProxy() {
super();
}
private static const _localTextFields:Array = [];
private static const _fontLibraryDependants:Array = [];
private static var _localText:ILocalText;
private static var compileTimeEnumeratedFonts:Vector.<Font> = Vector.<Font>(Font.enumerateFonts());
public static function queueLocalTextField(localTF:ILocalTextField) : void {
if(!_localText)
{
_localTextFields.push(localTF);
}
else
{
_localText.addLocalTF(localTF);
}
}
public static function queueFontLibraryDependant(dependant:IFontLibraryDependant) : void {
if((_localText) && (_localText.libraryInitialized))
{
dependant.onFontLibraryLoaded();
}
else if(_localText)
{
_localText.registerFontLibraryDependant(dependant);
}
else
{
_fontLibraryDependants.push(dependant);
}
}
public static function get initialized() : Boolean {
return !(_localText == null) && (_localText.libraryInitialized);
}
public static function get localText() : ILocalText {
if(!_localText)
{
throw new Error("An ILocalText implementation has not been set.");
}
else
{
return _localText;
}
}
public static function set localText(value:ILocalText) : void {
var localTF:LocalTextField = null;
var fontLibDependant:IFontLibraryDependant = null;
_localText = value;
for each(localTF in _localTextFields)
{
_localText.addLocalTF(localTF);
}
_localTextFields.length = 0;
for each(fontLibDependant in _fontLibraryDependants)
{
if(_localText.libraryInitialized)
{
fontLibDependant.onFontLibraryLoaded();
}
else
{
_localText.registerFontLibraryDependant(fontLibDependant);
}
}
_fontLibraryDependants.length = 0;
}
public static function get allFonts() : Vector.<Font> {
if(!_localText)
{
return compileTimeEnumeratedFonts;
}
return _localText.fontLibrary.allFonts;
}
}
}
I cannot for the life of me get this error resolves. If I just remove the code, I get no other errors (except for the same error in other files), so I know this is the last thing I need to get working. Any help would be greatly appreciated.
It's a Flash 9 file, set to publish with ActionScript 3.
Thanks!
This cannot be a Flash 9 file, because Vector type was introduced in Flash 10, and is not available in Flash 9 and below. So, set compile mode to Flash 10 and try again.

Castle and generics

Considering this code :
interface IRepository<T>
{
void Save();
}
class Repository<T>
{
public virtual void Save() // something
{ }
}
interface IOtherRepository : IRepository<OtherClass>
{
void Other();
}
class OtherRepository : Repository<OtherClass>, IOtherRepository
{
public override void Save() // something different
{ }
public override void Other(){ }
}
How is it possible to configure Castle Windsor to give me an instance of OtherRepository when I call container.Resolve<IRepository<OtherClass>> ?
If Castle Windsor can't do this, which ioc containers can ?
var container = new WindsorContainer();
container.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>));
container.Register(Component.For<IRepository<OtherClass>, IOtherRepository>()
.ImplementedBy<OtherRepository>());
var repo = container.Resolve<IRepository<Something>>();
Assert.IsInstanceOfType(typeof(Repository<Something>), repo);
var specificRepo = container.Resolve<IRepository<OtherClass>>();
Assert.IsInstanceOfType(typeof(OtherRepository), specificRepo);
var otherRepo = container.Resolve<IOtherRepository>();
Assert.AreSame(otherRepo, specificRepo);