Resource prefix v.01

This commit is contained in:
vakrilov
2015-03-10 18:08:17 +02:00
parent 320dd239ba
commit 81c5b76bc3
2 changed files with 41 additions and 21 deletions

View File

@@ -1,8 +1,12 @@
<Page> <Page>
<StackLayout> <StackLayout>
<Button horizontalAlignment="left" verticalAlignment="top" margin="10 0 10 20" text="top, left" /> <Label text="resource:" />
<Button horizontalAlignment="center" verticalAlignment="center" text="center, center" /> <Image url="res://icon" stretch="none" horizontalAlignment="left"/>
<Button horizontalAlignment="right" verticalAlignment="bottom" margin="0 30" text="bottom, right" />
<Button horizontalAlignment="stretch" verticalAlignment="stretch" margin="20" text="stretch, stretch" /> <Label text="url:" />
<Image url="http://www.google.com/images/errors/logo_sm_2.png" stretch="none" horizontalAlignment="left"/>
<Label text="app file:" />
<Image url="~/app/logo.png" stretch="none" horizontalAlignment="left" />
</StackLayout> </StackLayout>
</Page> </Page>

View File

@@ -13,34 +13,50 @@ var IMAGE = "Image";
var ISLOADING = "isLoading"; var ISLOADING = "isLoading";
var STRETCH = "stretch"; var STRETCH = "stretch";
var RESOURCE_PREFIX = "res://";
function isResource(value: string): boolean {
return value.indexOf(RESOURCE_PREFIX) === 0;
}
function isUrl(value: string): boolean {
return value.indexOf("http://") === 0 || value.indexOf("https://") === 0;
}
function isAppFile(value: string): boolean {
return value.indexOf("~/") === 0;
}
function isValidUrl(url: string): boolean { function isValidUrl(url: string): boolean {
var value = url ? url.trim() : ""; var value = url ? url.trim() : "";
return value !== "" && (value.indexOf("~/") === 0 || value.indexOf("http://") === 0 || value.indexOf("https://") === 0); return value !== "" && (isResource(value) || isAppFile(value) || isUrl(value));
} }
function onUrlPropertyChanged(data: dependencyObservable.PropertyChangeData) { function onUrlPropertyChanged(data: dependencyObservable.PropertyChangeData) {
var image = <Image>data.object; var image = <Image>data.object;
var value = data.newValue; var value: string = data.newValue;
if (isValidUrl(value)) { if (isValidUrl(value)) {
value = value.trim();
image.source = null; image.source = null;
image["_url"] = value; image["_url"] = value;
if (value !== "") { image._setValue(Image.isLoadingProperty, true);
image._setValue(Image.isLoadingProperty, true); if (isResource(value)) {
image.source = imageSource.fromResource(value.substr(RESOURCE_PREFIX.length));
if (value.trim().indexOf("~/") === 0) { image._setValue(Image.isLoadingProperty, false);
image.source = imageSource.fromFile(value.trim()); }
image._setValue(Image.isLoadingProperty, false); else if (isAppFile(value)) {
} else { image.source = imageSource.fromFile(value);
imageSource.fromUrl(value).then((r) => { image._setValue(Image.isLoadingProperty, false);
if (image["_url"] === value) { } else {
image.source = r; imageSource.fromUrl(value).then((r) => {
image._setValue(Image.isLoadingProperty, false); if (image["_url"] === value) {
} image.source = r;
}); image._setValue(Image.isLoadingProperty, false);
} }
});
} }
} }
} }